Results 1 to 3 of 3

Thread: Problems Creating layout on MainWindow

  1. #1
    Join Date
    Sep 2014
    Posts
    11
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Problems Creating layout on MainWindow

    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
    Qt Code:
    1. #include "cctv_form.h"
    2. #include <QPushButton>
    3. #include <QHBoxLayout>
    4.  
    5. cctv_form::cctv_form(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8. QPushButton *left = new QPushButton("Right",this);
    9. QPushButton *right = new QPushButton("Right",this);
    10.  
    11. QHBoxLayout *layout = new QHBoxLayout;
    12. layout->addWidget(left);
    13. layout->addWidget(right);
    14.  
    15. QWidget *Window = new QWidget();
    16. Window->setLayout(layout);
    17. this->setCentralWidget(window);
    18.  
    19.  
    20. }
    21.  
    22. cctv_form::~cctv_form()
    23. {
    24.  
    25. }
    To copy to clipboard, switch view to plain text mode 

    cctv_form.h
    Qt Code:
    1. #ifndef CCTV_FORM_H
    2. #define CCTV_FORM_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class cctv_form : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. cctv_form(QWidget *parent = 0);
    12. ~cctv_form();
    13. };
    14.  
    15. #endif // CCTV_FORM_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "cctv_form.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. cctv_form w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    Can anyone point me in the right direction ?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problems Creating layout on MainWindow

    Strange.

    Try creating the buttons with their actual parent instead.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,162
    Thanks
    296
    Thanked 858 Times in 845 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problems Creating layout on MainWindow

    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.

Similar Threads

  1. Mainwindow layout change
    By Anshuman in forum Qt Programming
    Replies: 1
    Last Post: 28th April 2011, 02:33
  2. Replies: 12
    Last Post: 18th April 2011, 16:24
  3. Replies: 0
    Last Post: 18th January 2011, 17:59
  4. Mainwindow layout problem with QDockWidget.
    By pastispast in forum Qt Programming
    Replies: 3
    Last Post: 13th January 2011, 07:04
  5. mainwindow layout vs widget layout
    By fatecasino in forum Newbie
    Replies: 2
    Last Post: 14th December 2010, 15:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.