当前位置: 代码迷 >> QT开发 >> QT窗口有关问题
  详细解决方案

QT窗口有关问题

热度:38   发布时间:2016-04-25 03:34:37.0
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()));
  相关解决方案