Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt-QStackedLayout setCurrentIndex显示空白窗口,而不是小部件_Qt - Fatal编程技术网

Qt-QStackedLayout setCurrentIndex显示空白窗口,而不是小部件

Qt-QStackedLayout setCurrentIndex显示空白窗口,而不是小部件,qt,Qt,几天前我刚开始学习Qt来制作游戏,我正试图找出如何让布局工作 我想要的是一个使用QStackedLayout的窗口,里面有两个小部件:StartScreen和GameScreen。StartScreen在程序运行时显示在顶部。StartScren中的一个按钮连接到窗口内的一个函数,窗口将调用setCurrentIndex将顶部的小部件更改为GameScreen 现在发生的事情是,当我点击按钮时,视图会变成一个空白窗口。我尝试过硬编码setCurrentIndex(0),它什么都不做,setCur

几天前我刚开始学习Qt来制作游戏,我正试图找出如何让布局工作

我想要的是一个使用QStackedLayout的窗口,里面有两个小部件:StartScreen和GameScreen。StartScreen在程序运行时显示在顶部。StartScren中的一个按钮连接到窗口内的一个函数,窗口将调用setCurrentIndex将顶部的小部件更改为GameScreen

现在发生的事情是,当我点击按钮时,视图会变成一个空白窗口。我尝试过硬编码setCurrentIndex(0),它什么都不做,setCurrentIndex(1),它是游戏屏幕应该是什么,并显示相同的空白窗口,以及setCurrentIndex(2),它超出了索引范围,但仍然什么都不做。所以连接正在进行,但我不明白为什么会出现一个空白窗口而不是我在游戏屏幕上的按钮

如果有人能向我解释我遗漏了什么概念以及如何修复它,我将不胜感激。谢谢

下面是window.cpp:

Window::Window(QWidget *parent) : QWidget(parent)
{
  resize(640, 480);

  layout = new QStackedLayout;

  createStartScreen();
  createGameScreen();
  setLayout(layout);

  show();

};

void Window::createStartScreen(){

  start = new StartScreen();

  layout->addWidget(start);

  start->setWindow(this);

}

void Window::playGame(){

  layout->setCurrentIndex(layout->indexOf(game));

}

void Window::createGameScreen(){

  game = new GameScreen();

  layout->addWidget(game);
}
startscreen.cpp:

StartScreen::StartScreen(QWidget *parent) : QWidget(parent)
{
  newGameButton = new QPushButton("New Game", this);
  newGameButton->setGeometry(QRect(QPoint(260, 300), QSize(120,40)));

  quitButton = new QPushButton("Quit", this);
  quitButton->setGeometry(QRect(QPoint(260, 360), QSize(120,40)));

  connect(quitButton, SIGNAL(clicked()), QApplication::instance(), SLOT(quit()));

};

void StartScreen::setWindow(Window *w){

  connect(newGameButton, SIGNAL(clicked()), w, SLOT(playGame()));

}
GameScreen::GameScreen(QWidget *parent) : QWidget(parent)
{
  button = new QPushButton("Hi");
  button->setGeometry(QRect(QPoint(260, 260), QSize(120,40)));
};
gamescreen.cpp:

StartScreen::StartScreen(QWidget *parent) : QWidget(parent)
{
  newGameButton = new QPushButton("New Game", this);
  newGameButton->setGeometry(QRect(QPoint(260, 300), QSize(120,40)));

  quitButton = new QPushButton("Quit", this);
  quitButton->setGeometry(QRect(QPoint(260, 360), QSize(120,40)));

  connect(quitButton, SIGNAL(clicked()), QApplication::instance(), SLOT(quit()));

};

void StartScreen::setWindow(Window *w){

  connect(newGameButton, SIGNAL(clicked()), w, SLOT(playGame()));

}
GameScreen::GameScreen(QWidget *parent) : QWidget(parent)
{
  button = new QPushButton("Hi");
  button->setGeometry(QRect(QPoint(260, 260), QSize(120,40)));
};

这是因为你们并没有按“显示”按钮。但是您应该使用布局来处理它

e、 g:

按钮的大小将与其文本相适应,并将在游戏屏幕中定位


另一方面,您应该向StartScreen添加一个信号,请求新游戏并在窗口中进行连接。这样就不会在StartScreen和Window之间产生紧密耦合。

我在执行您的建议时发现了问题,我没有在QPushButton内的参数中添加“this”。我还尝试使用button->show(),但它导致按钮出现在单独的窗口中。您对联轴器的反馈非常有价值,我将用我的程序来改变这一点。谢谢你的回答!