简体   繁体   English

Qt程序不显示

[英]Qt program doesn't display

Hi I'm trying to make a simple layout in Qt and first of all the layout is not working properly at all, all that showed up was a cancel button. 嗨,我正在尝试在Qt中制作一个简单的布局,首先,布局根本无法正常工作,显示的全部是一个取消按钮。 So I have been messing around and now when I run it it runs without errors but no window pops up, don't know what I could have done to cause this? 所以我一直在搞乱,现在当我运行它时它没有错误但没有弹出窗口,不知道我该怎么办才能引起这种情况? Here is my code 这是我的代码

#ifndef FILMINPUT_H
#define FILMINPUT_H

#include <QMainWindow>
#include "Film.h"
#include "FilmWriter.h"
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>

namespace Ui {
    class FilmInput;
}

class FilmInput : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::FilmInput *ui;
    //widgets
    QMainWindow* window;
    QMenuBar* menubar;
    QLabel* infoLabel;
    QLabel* titleLabel;
    QLabel* durationLabel;
    QLabel* directorLabel;
    QLabel* relDateLabel;
    QTextEdit* titleEdit;
    QTextEdit* durationEdit;
    QTextEdit* directorEdit;
    QTextEdit* relDateEdit;
    QPushButton* saveBtn;
    QPushButton* cancelBtn;
    Film f;
    //sets up gui and connects signals and slots
    void setUpGui();
};

#endif // FILMINPUT_H

#include "filminput.h"
#include "ui_filminput.h"
#include <QtGui>


FilmInput::FilmInput(QWidget *parent) :
    QMainWindow(parent),
   ui(new Ui::FilmInput)
{
    ui->setupUi(this);
    setUpGui();
}

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

void FilmInput::setUpGui(){
    //initialise widgets
    infoLabel = new QLabel("Please enter film data which will be saved to a file",this);
    titleLabel = new QLabel("Film Title",this);
    durationLabel = new QLabel("Film Duration",this);
    directorLabel = new QLabel("Film Director",this);
    relDateLabel = new QLabel("Film Release Date",this);
    titleEdit = new QTextEdit(this);
    durationEdit = new QTextEdit(this);
    directorEdit = new QTextEdit(this);
    relDateEdit = new QTextEdit(this);
    saveBtn = new QPushButton("Save Film",this);
    cancelBtn = new QPushButton("Cancel",this);
    //set layout
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setMenuBar(menubar);
    layout->addWidget(infoLabel);
    layout->addWidget(titleLabel);
    layout->addWidget(durationLabel);
    layout->addWidget(directorLabel);
    layout->addWidget(relDateLabel);
    layout->addWidget(titleEdit);
    layout->addWidget(durationEdit);
    layout->addWidget(directorEdit);
    layout->addWidget(relDateEdit);
    layout->addWidget(saveBtn);
    layout->addWidget(cancelBtn);

    this->setLayout(layout);
    this->setWindowTitle("Film Archive");
}

#include <QtGui/QApplication>
#include "filminput.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    FilmInput w;
    w.show();

    return a.exec();
}

It looks like you have conflicting things here. 您似乎在这里遇到矛盾的事情。 You have Qt's WYSIWYG widget editor (QtDesigner), which you tell Qt to initialize ( ui->setupUi(this) ): 您具有Qt的WYSIWYG小部件编辑器(QtDesigner),您可以告诉Qt进行初始化( ui->setupUi(this) ):

#include "ui_filminput.h" //<---- Generated from the QtDesigner form 

FilmInput::FilmInput(QWidget *parent) :
    QMainWindow(parent),
   ui(new Ui::FilmInput)  //<---- Creating the struct that holds of the widget pointers.
{
    ui->setupUi(this); //<---- Telling Qt to setup and layout all the QtDesigner widgets from this designer form.
    //setUpGui();   <--- Where your layouts and widgets are accidentally clashing with the form's widgets.
}

Then you also have the ones you manually are creating inside setUpGui() . 然后,您还可以在setUpGui()内部手动创建那些。 It's fine to mix the QtDesigner forms with manually created widgets - I do it all the time. 可以将QtDesigner表单与手动创建的小部件混合使用-我一直都这样做。 But what you accidentally are doing is you are accidentally setting a layout: 但是,您不小心在做什么是您不小心设置了布局:

this->setLayout(layout);

On this main window.... which the QtDesigner form already did for its widgets, overwriting them, and possibly confusing the layout of the main window. 在此主窗口上。...QtDesigner表单已对其小部件进行了处理,覆盖了它们,并可能使主窗口的布局混乱。

You can either remove the QtDesigner widgets entirely, or prefereably, make them interact nicely by setting your layout on a subwidget of your main window. 您可以完全删除QtDesigner窗口小部件,也可以通过在主窗口的子窗口小部件上设置布局来使它们很好地交互。

You can access the QtDesigner widgets through the 'ui' member-variable. 您可以通过“ ui”成员变量访问QtDesigner小部件。

this->ui->someNameOfWidgetInQtDesigner

I believe the main window has a widget already created in QtDesigner called "centralWidget", or something similar (open up FilmInput.ui and check the actual naming). 我相信主窗口有一个已经在QtDesigner中创建的名为“ centralWidget”的小部件,或类似的小部件(打开FilmInput.ui并检查实际的命名)。 So you should set your layout on that, assuming you didn't already create a layout in QtDesigner. 因此,假设您尚未在QtDesigner中创建布局,则应以此为基础设置布局。

this->ui->centralWidget->setLayout(layout);

If your QtDesigner form ( FilmInput.ui ) already has a layout set on the centralWidget, add a new QWidget in the designer form as a child of the centralWidget in centralWidget's layout, and name it something like 'sidePanel' or whatever makes sense, and then do: 如果您的QtDesigner表单( FilmInput.ui )已经在centralWidget上设置了布局,则在设计器表单中添加一个新的QWidget作为centralWidget布局中centralWidget的子代,并命名为“ sidePanel”或其他有意义的名称,然后然后做:

this->ui->sidePanel->setLayout(layout);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM