【问题标题】:Getting a QDialog to show up with show or exec functions让 QDialog 与 show 或 exec 函数一起显示
【发布时间】:2014-04-30 22:12:38
【问题描述】:

现在我创建的 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 对话框后,对话框中不会显示任何 lineedits 和组合框。

编辑:

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

【问题讨论】:

  • 嗨,show() 以非模态打开对话框并立即返回,exec() 以模态打开对话框并等待返回值 您是否尝试显示 numberPlayers 的值? qDebug()
  • 是的,值很好,我遇到的问题是我为 playerInfo 对话框创建的所有小部件都没有显示
  • 它本质上是一个空白对话框(空白框)
  • 请您发布您的 playerInfo.h,我不会重现您的问题
  • 真的没有看到错误,你确定你看到的窗口不是你的主窗口吗?你应该在声明它为测试后立即尝试 pInfo.exec()

标签: c++ qt user-interface qdialog


【解决方案1】:

对我来说效果很好。由于您还没有发布numPlayers 类的代码,我初始化了int numberPlayers = 3;playerInfo 对话显示三次,然后出现主窗口。看起来您看到的窗口是主窗口。可能是numberPlayers = pPlayers.returnInput(); 返回0。添加qDebug() &lt;&lt; numberPlayers; 以查看您是否获得了正值。

【讨论】:

  • 是的,我收到的 numberPlayers 值是一个与用户从输入对话框中选择 2 - 4 人相关的数字
  • numPlayers 类只是一个包含 QInputDialog 的对话框,提示用户输入 2 - 4 之间的数字
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多