PDA

View Full Version : Problems Creating layout on MainWindow



Ion
23rd September 2014, 17:06
Ok currently just testing a basic implementation as having problems with this section of the code in my main program.

Below is a simple class that just contains a couple of buttons created in code. However when I try to add them to either the central widget or to a layout the code fails with


QWidget::setLayout: Attempting to set QLayout "" on cctv_form "", which already has a layout

cctv_form.cpp

#include "cctv_form.h"
#include <QPushButton>
#include <QHBoxLayout>

cctv_form::cctv_form(QWidget *parent)
: QMainWindow(parent)
{
QPushButton *left = new QPushButton("Right",this);
QPushButton *right = new QPushButton("Right",this);

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(left);
layout->addWidget(right);

QWidget *Window = new QWidget();
Window->setLayout(layout);
this->setCentralWidget(window);


}

cctv_form::~cctv_form()
{

}

cctv_form.h

#ifndef CCTV_FORM_H
#define CCTV_FORM_H

#include <QMainWindow>

class cctv_form : public QMainWindow
{
Q_OBJECT

public:
cctv_form(QWidget *parent = 0);
~cctv_form();
};

#endif // CCTV_FORM_H


main.cpp

#include "cctv_form.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
cctv_form w;
w.show();

return a.exec();
}


Can anyone point me in the right direction ?

anda_skoa
23rd September 2014, 19:49
Strange.

Try creating the buttons with their actual parent instead.

Cheers,
_

d_stranz
24th September 2014, 02:02
Are you using Qt Designer to create your MainWindow .ui file? Are you specifying a layout in that for the central widget?

Is this your actual code? You have a QWidget instance named "Window" (with an uppercase "W") and you're calling setCentralWidget( window ) (with a lowercase "w"). Unless you have lowercase "window" declared somewhere, the code you posted won't compile.