使QDialog显示show或exec函数
Getting a QDialog to show up with show or exec functions
现在我创建的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
对话显示三次,然后出现主窗口。看起来你看到的窗口就是主窗口。可能是返回0
的numberPlayers = pPlayers.returnInput();
。加上qDebug() << numberPlayers;
,看看你是否得到了一个正值。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- Python 的 exec() 函数的 C++ 版本
- PHP 网站在传递图像作为参数时未运行 exec 函数
- 将 PHP 中的 $_POST 变量传递给由 exec() 函数运行的C++程序
- 如何在 exec 循环停止后正确使用 QSharedPointer 的析构函数<QObject>?
- 使用CreateProcess尽可能使用单个命令参数调用exec*函数
- 在参数中传递给 exec*() 函数系列的内存会发生什么情况
- 如何在linux中使用c或c++在不使用system()或exec()函数的情况下调用像pwd或ls-l这样的系统函数
- 如何在c++中对exec()进行析构函数友好的调用
- 禁用C和Pascal中的system()和exec()函数
- c++通过exec函数杀死子进程,但不杀死父进程和正常运行时间
- 如何在c++中实现Python函数exec()
- 使QDialog显示show或exec函数