QT窗口问题
大家好 小弟初学QT。正在写一个QT的代码。下面是我的main.cpp的一部分:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
app.setApplicationName("test");
// Show Preferences:
Preferences prefs;
prefs.exec();
GenericClient *client;
if (prefs.wantEkm())
{
// Poll the node for data:
client = new EkmClient(prefs.host(), prefs.ekmPort(), prefs.interval(), &app);
}
else if (prefs.wantUdp())
{
// Open a port for incoming data:
client = new UdpClient(prefs.udpPort());
}
else // no valid client -> exit
{
return 1;
}
// CLIENT ----> AVERAGER
// Create the data averaging block:
Averager averager(prefs.averageValues() ? prefs.numAvgValues() : 1);
QObject::connect(client, SIGNAL(dataReceived(SimpleDataNode)),
&averager, SLOT(dataArrived(SimpleDataNode)));
// AVERAGER ----> GRAPHS, LOGGER, TABLE
// Start the logger if the user wants that:
DataLogger logger(prefs);
QObject::connect(&averager, SIGNAL(dataReady(SimpleDataNode)),
&logger, SLOT(logData(SimpleDataNode)));
// Create the bar-graph window:
BarGraphWindow bargraphwindow(prefs);
QObject::connect(&averager, SIGNAL(dataReady(SimpleDataNode)),
&bargraphwindow, SLOT(showData(SimpleDataNode)));
// Create the graphing window:
LineGraphWindow linegraphwindow(prefs);
QObject::connect(&averager, SIGNAL(dataReady(SimpleDataNode)),
&linegraphwindow, SLOT(showData(SimpleDataNode)));
// Create the table window:
TableWindow tablewindow(prefs);
QObject::connect(&averager, SIGNAL(dataReady(SimpleDataNode)),
&tablewindow, SLOT(showData(SimpleDataNode)));
// Show the windows:
bargraphwindow.show();
linegraphwindow.show();
tablewindow.show();
app.exec();
return 0;
}
prefs是主窗口,程序运行后就会打开。bargraphwindow,linegraphwindow,tablewindow是副窗口,在主窗口调整好参数之后打开。现在的问题是如果我关闭主窗口,3个副窗口依然会打开。这个问题如何解决?就是关闭主窗口就相当于是关闭整个程序。还有就是打开三个副窗口后主窗口就会自动关闭,如何避免主窗口自动关闭?
多谢大家!
[解决办法]
#include <qdialog.h>
#include <qlayout.h>
class CDialog : public QDialog
{
Q_OBJECT
public:
CDialog(QWidget *parent = NULL) : QDialog(parent)
{
QPushButton *ppbOk = new QPushButton("Ok", this);
QPushButton *ppbCancel = new QPushButton("Cancel", this);
QHBoxLayout *phLayout = new QHBoxLayout(this);
phLayout->addWidget(ppbOk);
phLayout->addWidget(ppbCancel);
connect(ppbOk, SIGNAL(clicked()), this, SLOT(clickedOk()));
connect(ppbCancel, SIGNAL(clicked()), this, SLOT(clickedCancel()));
}
private slots:
void clickedOk()
{
accept();
}
void clickedCancel()
{
close();
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
CDialog dlg;
if (dlg.exec() != QDialog::Accepted)
{
return 0;
}
QWidget w;
w.show();
return a.exec();
}