使QDialog显示show或exec函数

Getting a QDialog to show up with show or exec functions

本文关键字:exec 函数 show QDialog 显示      更新时间:2023-10-16

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

我的主.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();
}

播放器信息对话框:(我很难出现的一个(

#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对话框后,对话框中不会显示任何行编辑和组合框。

编辑:

播放器信息.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

它对我来说还可以。由于你还没有发布numPlayers类的代码,我初始化了int numberPlayers = 3;playerInfo对话显示三次,然后出现主窗口。看起来你看到的窗口就是主窗口。可能是返回0numberPlayers = pPlayers.returnInput();。加上qDebug() << numberPlayers;,看看你是否得到了一个正值。