2013-07-22 2 views
0

안녕하세요. Qt에서 간단한 레이아웃을 만들기 위해 노력하고 있습니다. 먼저 모든 레이아웃이 제대로 작동하지 않습니다. 모두 취소 버튼이었습니다. 그래서 나는 주위를 어지럽 혔고 지금은 그것을 실행하면 오류없이 실행되지만 창문이 튀어 나옵니다. 왜 이런 일을 할 수 있었는지 모르십니까? 다음은 여기에 충돌하는 일이 마치 내 코드Qt 프로그램이 표시되지 않습니다.

#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(); 
} 

답변

4

입니다. 당신은 Qt는이 (ui->setupUi(this))를 초기화하는 말 Qt의 WYSIWYG 위젯 편집기 (QtDesigner)이 있습니다

#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. 
} 

그런 다음 당신은 또한 수동 setUpGui() 내에서 작성하는 사람이 있습니다. QtDesigner 양식과 수작업으로 만든 위젯을 혼합하여 사용해도 좋습니다. 항상 그렇게합니다. 이 메인 창에서

this->setLayout(layout); 

.... QtDesigner 양식이 이미 위젯했던 덮어 쓰기를, 가능의 레이아웃을 혼란 :하지만 실수하고있는 것은 당신이 실수로 레이아웃을 설정하는 것입니다 메인 윈도우.

QtDesigner 위젯을 완전히 제거하거나 메인 윈도우의 하위 위젯에 레이아웃을 설정하여 상호 작용이 잘되도록 할 수 있습니다.

'ui'멤버 변수를 통해 QtDesigner 위젯에 액세스 할 수 있습니다.

this->ui->someNameOfWidgetInQtDesigner 

나는 메인 윈도우는 "centralWidget"라고 이미 QtDesigner에서 만든 위젯, 또는 비슷한 ( FilmInput.ui을 열고 실제 이름을 확인) 가지고 있다고 생각합니다. QtDesigner에서 아직 레이아웃을 만들지 않았다는 가정하에 레이아웃을 설정해야합니다.

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

당신의 QtDesigner 양식 (FilmInput.ui가) 이미 centralWidget에 설정 레이아웃이있는 경우, centralWidget의 레이아웃에 centralWidget의 자식으로 디자이너의 형태로 새로운는 QWidget을 추가하고 이름이 뭔가 'sidePanel'또는 같은 어떤 의미가 있든간에 다음을 수행하십시오.

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

도움을 주셔서 감사합니다. 이제 setUpGui 기능을 어디에서 호출해야합니까? – Dmon

+0

그 위치를 선언 할 수는 있지만, 이미 FilmInput에 레이아웃이 설정되어 있기 때문에 함께 상호 작용할 수 있도록 레이아웃을 FilmInput의 하위 요소 인 새 위젯으로 설정해야합니다. QtDesigner에서 FilmInput에 위젯을 추가하고 (아직 비어 있지 않은 경우) setUpGui()에서 새 위젯을 사용하여 레이아웃을 설정합니다. –

+0

코드 -1073741819로 종료되었습니다 위젯을 위젯으로 추가 한 다음이 코드를 this-> ui-> widget-> setLayout (layout);으로 변경했습니다. 나는 문제가 FilmInput :: FilmInput (QWidget * parent)에 있다고 생각한다. QMainWindow (부모), ui (새로운 Ui :: FilmInput) { ui-> setupUi (this); setUpGui(); } – Dmon

관련 문제