让QDialog显示show或exec函数

时间:2014-04-30 22:12:39

标签: c++ qt user-interface qdialog

现在我创建的QDialog没有显示出来。有什么办法可以帮助我找到错误吗?

我的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对话:(我难以出现的那个)

#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();
}

如果我能提供更多可以帮助调试过程的信息,请告诉我。谢谢你的帮助。

我读了一些使用show()代替exec()的其他示例。有区别吗?现在,在输入numberPlayers对话框后,对话框中没有任何线条和组合框出现。

编辑:

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

1 个答案:

答案 0 :(得分:0)

对我来说这很好用。由于您尚未发布numPlayers课程的代码,因此我初步了int numberPlayers = 3;playerInfo对话显示三次,然后出现主窗口。看起来您所看到的窗口是主窗口。可能会numberPlayers = pPlayers.returnInput();返回0。添加qDebug() << numberPlayers;以查看您是否获得正值。