简体   繁体   English

让QDialog与show或exec函数一起显示

[英]Getting a QDialog to show up with show or exec functions

Right now the QDialog I created is not showing up. 现在,我创建的QDialog没有出现。 Is there any way you can help me find the error? 有什么办法可以帮助我找到错误?

My main.cpp 我的main.cpp

#include <QApplication>
#include "numplayers.h"
#include "playerinfo.h"
#include "mainwindow.h"

int main( int argv, char* argc[] ) {
    int numberPlayers;
  QApplication app( argv, argc );
  MainWindow mw;
  numPlayers pPlayers;
   playerInfo pInfo;
  if (pPlayers.exec())
  {
    numberPlayers = pPlayers.returnInput();
  }

  for (int i = 0; i < numberPlayers; i++)
  {
    if(pInfo.exec())
    {
      int index = pInfo.getIndex();
      QString name = pInfo.getName();
          // do something with these..
      mw.setPlayerData(index, name, i);
    }
  }
  mw.setGUIWidgets(numberPlayers);
  mw.createCentralWidget();
  mw.show();

  return app.exec();
}

playerInfo Dialog: (the one I am having trouble to get show up) playerInfo对话框:(我无法显示的对话框)

#include "playerinfo.h"
#include <QDialogButtonBox>
#include <QLayout>
#include <QComboBox>
#include <QLineEdit>


playerInfo::playerInfo(QWidget *parent) :
    QDialog(parent)
{
    QVBoxLayout *layout = new QVBoxLayout;
    this->setLayout(layout);

    lineEdit = new QLineEdit; // create line edit
    layout->addWidget(lineEdit);

    comboBox = new QComboBox; // create combo box and add items to it
    QStringList items = QStringList() << "Hat" << "Car" << "Shoe" << "SpaceShip" << "Basketball" << "Ring";
    comboBox->addItems(items);
    layout->addWidget(comboBox);



    // create button box
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    layout->addWidget(buttonBox);
}

QString playerInfo::getName() const
{
    return lineEdit->text();
}

int playerInfo::getIndex() const
{
    return comboBox->currentIndex();
}

if there is any more information I can provide that could help with the debugging process please let me know. 如果有更多我可以提供的有助于调试过程的信息,请告诉我。 Thank you for all the help. 感谢您的所有帮助。

I read some other examples that used show() instead of exec() . 我读了其他一些使用show()而不是exec()示例。 Is there a difference? 有区别吗? Right now after the numberPlayers dialog is entered none of the lineedits and comboboxes show up in the dialog. 现在,在输入numberPlayers对话框后,对话框中将不会显示任何行编辑和组合框。

Edit: 编辑:

playerInfo.h playerInfo.h

#ifndef PLAYERINFO_H
#define PLAYERINFO_H
#include <QDialogButtonBox>
#include <QLayout>
#include <QComboBox>
#include <QLineEdit>
#include <QDialog>
#include <QWidget>

class QLineEdit;
class QComboBox;

class playerInfo : public QDialog
{
    Q_OBJECT
public:
    explicit playerInfo(QWidget *parent = 0);
    QString getName() const;
    int getIndex() const;

private:
    int max_players;
    QVBoxLayout *layout ;
    QDialogButtonBox *buttonBox;
    QComboBox *comboBox;
    QLineEdit *lineEdit;
};

#endif // MYDIALOG_H

It worked OK for me. 它对我来说很好。 As you haven't posted the code of numPlayers class, I initialised int numberPlayers = 3; 由于您尚未发布numPlayers类的代码, numPlayers我初始化了int numberPlayers = 3; . playerInfo dialogue was shown thrice and then main window appeared. playerInfo对话框显示三次,然后出现主窗口。 It looks like the window you are seeing is the main window. 看起来您正在看到的窗口是主窗口。 May be numberPlayers = pPlayers.returnInput(); 可能是numberPlayers = pPlayers.returnInput(); returning 0 . 返回0 Add qDebug() << numberPlayers; 添加qDebug() << numberPlayers; to see if you are getting a positive value. 看看您是否获得了正值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM