0

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

1 Answer 1

4

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)):

#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(). It's fine to mix the QtDesigner forms with manually created widgets - I do it all the time. 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.

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.

You can access the QtDesigner widgets through the 'ui' member-variable.

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). So you should set your layout on that, assuming you didn't already create a layout in 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:

this->ui->sidePanel->setLayout(layout);
Sign up to request clarification or add additional context in comments.

11 Comments

great thanks for the help, where should I call the setUpGui function now that it is clashing with the ui->setupui?
You can declare it where it is, but since FilmInput already has a layout set to it, you'll need to set the layout to a new widget that is a child of FilmInput, so they can interact together. In QtDesigner, add a widget to FilmInput (if there isn't already an empty one there), and in setUpGui() use that new widget to set your layout to.
I get an exited with code -1073741819 I added a widget just left the name as widget then changed code to this->ui->widget->setLayout(layout); I think the problem is at FilmInput::FilmInput(QWidget *parent) : QMainWindow(parent), ui(new Ui::FilmInput) { ui->setupUi(this); setUpGui(); }
Could you post your updated code? I'll take a look at it and see.
looks like I might have a problem with minGW, going to try see if that's my problem before I waste your time
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.