Qt - 未显示子窗口小部件

时间:2016-06-30 11:53:51

标签: c++ qt

我构建了一个多窗口应用程序,到目前为止创建并显示了8个按钮的MainWidget。我的下一步是让每个按钮打开一个新窗口,在Qt术语中是QWidget的子窗口。我在QVectors中保留所有按钮和新窗口(应该在点击按钮时打开)。这一切都没有警告或错误编译,但是,当我单击一个按钮时,相应的QWidget窗口不会显示。

mainwidget.h

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QGridLayout>
#include <QSignalMapper>

#include "examwindow.h"


namespace Ui { class MainWidget; }

class MainWidget : public QWidget {
    Q_OBJECT

public:
    explicit MainWidget(QWidget *parent = 0);
    ~MainWidget();

private:
    Ui::MainWidget *ui;

    int nExams;
    QVector<QString> titles;
    QVector<QPushButton*> examButtons;
    QGridLayout* mainWidgetLayout;
    QVector<ExamWindow*> examWindows;

public slots:
    void clickedExamW (); 
};

#endif // MAINWIDGET_H

mainwidget.cpp

#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QDesktopWidget>
#include <iostream>
MainWidget::MainWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    /**
     * @brief Resize the main window size in proportion to the desktop size
     */
    double ratio = 0.7;
    resize(QDesktopWidget().availableGeometry(this).size() * ratio);

    /**
     * @brief Set the main window position in the desktop center
     */
    QDesktopWidget dw;
    int width = this->frameGeometry().width();
    int height = this->frameGeometry().height();
    int screenWidth = dw.screen() -> width();
    int screenHeight = dw.screen() -> height();
    this->setGeometry((screenWidth / 2) - (width / 2), (screenHeight / 2) - (height / 2), width, height);

    /**
     * Set the button titles
     */
    titles.push_back("FCE - 2008");
    titles.push_back("CAE - 2008");
    titles.push_back("CPE - 2008");
    titles.push_back("ЕГЭ");
    titles.push_back("FCE - 2015");
    titles.push_back("CAE - 2015");
    titles.push_back("CPE - 2015");
    titles.push_back("User's Format");

    /**
     * Create buttons
     */
    nExams = 8;         // Number of exams

    examButtons.resize(nExams);
    for(int i = 0; i < nExams; i++) {
        examButtons[i] = new QPushButton(titles[i]);
        examButtons[i]->setMinimumSize(QSize(150, 150));
        examButtons[i]->setMaximumSize(QSize(500, 500));
        examButtons[i]->setObjectName(titles[i]);
        connect(examButtons[i], SIGNAL(clicked()), this, SLOT(clickedExamW()));
    }

    /**
     * Add exam buttons to the main widget layout
     */
    mainWidgetLayout = new QGridLayout(this);
    for(int i = 0; i < nExams; i++)
        if (i < nExams / 2)
            mainWidgetLayout -> addWidget(examButtons[i], i, 0);
        else
            mainWidgetLayout -> addWidget(examButtons[i], i - nExams / 2, 1);

    /**
     * Create exam windows
     */
    examWindows.resize(nExams);
    for(int i = 0; i < nExams; i++) {
        examWindows[i] = new ExamWindow(this);
        examWindows[i]->setWindowTitle(titles[i]);
    }



}

void MainWidget::clickedExamW() {

    QObject *senderObj = sender();
    QString senderObjName = senderObj->objectName();
    for(int i = 0; i < nExams; i++)
        if (senderObjName == titles[i]) {
            this->setWindowTitle(titles[i]); // WORKS - it changes the title
            examWindows[i]->show();          // DOES NOT WORK - no win shown 
        }
}


MainWidget::~MainWidget()
{
    delete ui;
}

examwindow.h

#ifndef EXAMWINDOW_H
#define EXAMWINDOW_H

#include <QWidget>

class ExamWindow : public QWidget
{
    Q_OBJECT
public:
    explicit ExamWindow(QWidget *parent = 0);

signals:

public slots:

};

#endif // EXAMWINDOW_H

examwindow.cpp

#include "examwindow.h"

ExamWindow::ExamWindow(QWidget *parent) : QWidget(parent)
{

}

2 个答案:

答案 0 :(得分:3)

方式,如何创建小部件是可以的(examWindows[i] = new ExamWindow(this);)。但是:

ExamWindow::ExamWindow(QWidget *parent)
  : QWidget(parent, Qt::Window)
{}

您需要直接specify a flag您需要一个&#34;窗口&#34;窗口小部件。

或者,如果您不想设置父窗口小部件,则可以为自动释放内存设置Qt::WA_DeleteOnClose属性。请注意,在这种情况下,您的examWindows向量中将包含无效指针。如果您将使用下一个声明,可以解决:QVector<QPointer<ExamWindow>> examWindows;

答案 1 :(得分:0)

答案:事实证明问题出在这一行:

examWindows[i] = new ExamWindow(this);

如果我删除了这个&#39;,使我的Windows无父母,它按预期工作。我很困惑为什么,我想我现在必须手动删除它们。