Results 1 to 9 of 9

Thread: Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)

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

    Default Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)

    Hi there, currently not looking to make a custom widget for this as it is only a layout class object. What I want to do is be able to create a pane/window or whatever i need so that I can place a QWidget class (that i created in code instead of on the form) inside so that I can adjust the positions etc on Qt Designer.

    So need two bits of information, one what item in Qt designer should i use to allow me to place a QWidget inside it?

    and two, how do i then insert my class object into this window/pane ?

    Layout class constructor listed below:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QPalette p(palette());
    8. p.setColor(QPalette::Background, Qt::lightGray);
    9. this->setAutoFillBackground(true);
    10. this->setPalette(p);
    11. this->resize(1000,500);
    12. this->setWindowTitle(QApplication::translate("toplevel", "CCTV"));
    13.  
    14. mplayer_wrapper *wrapper = new mplayer_wrapper(this);
    15. label = new QLabel(this);
    16. Timer = new QTimer(this);
    17.  
    18.  
    19. button1 = new QPushButton("Stop State");
    20. button2 = new QPushButton("Start Stream");
    21. button3 = new QPushButton("Time");
    22. vlayout = new QVBoxLayout();
    23.  
    24. vlayout->addWidget(wrapper);
    25. vlayout->addWidget(button1);
    26. vlayout->addWidget(button2);
    27. vlayout->addWidget(button3);
    28. vlayout->addWidget(label);
    29.  
    30. ui->centralWidget->setLayout(vlayout);
    31.  
    32. connect(button1, &QPushButton::clicked,wrapper,&mplayer_wrapper::stop_mplayer);
    33. connect(button2, &QPushButton::clicked,wrapper,&mplayer_wrapper::start_mplayer);
    34. //connect(button3, &QPushButton::clicked,this, &MainWindow::show_time);
    35. connect(Timer,&QTimer::timeout,this,&MainWindow::show_time);
    36.  
    37. //ui->widget->
    38.  
    39. Timer->start();
    40.  
    41. }
    To copy to clipboard, switch view to plain text mode 

    Specifically in regards to this code I want to insert mplayer_wrappers object wrapper. 90% of this code I want to do on the designer as it's becoming a nightmare when the layout is alot more complicated.
    Last edited by Ion; 18th September 2014 at 15:13.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)

    The easiest way to insert custom widgets into designer forms is to use the "Promote to" functionality of Designer. Simply drag & drop a widget that is the base class of your custom widget then right click it, choose "Promote to" and follow the instructions. When C++ code is generated from the form, the stand-in you placed on the form will be replaced by your custom widget class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2012
    Location
    Warsaw, Poland
    Posts
    37
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)


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

    Default Re: Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)

    So is there no way of doing this without creating a custom widget? As seem all of your responses have been about creating a custom widget which wasn't want I wanted.

    Isn't there any way I can just insert the QWidget class into the widget and tell it to fill the whole area of "widget" (say something like setting the QWidget class as a child of widget ? and telling it to fill the area ?).
    Last edited by Ion; 18th September 2014 at 15:51.

  5. #5
    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: Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)

    You can just instantiate the widget and add it to the container's layout if it has one or create the layout in code and then add the new child widget

    Cheers,
    _

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)

    Quote Originally Posted by Ion View Post
    Isn't there any way I can just insert the QWidget class into the widget and tell it to fill the whole area of "widget" (say something like setting the QWidget class as a child of widget ? and telling it to fill the area ?).
    Sure there is (you can drag & drop a widget called "Widget", put it in a layout and set its size policy to expanding) but what is the point of doing that?

    You said you wanted to put a widget you created in code in there, so using the Promote feature is the easiest way of doing exactly that -- you create a widget in code, call it "MyWidget" and promote a widget in the form to "MyWidget" so that when your program is built, it contains a MyWidget instance instead of whatever you placed in the form. This way you can adjust all the properties MyWidget shares with whatever you placed on the form directly in Designer.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)

    I think the OP's problem might be that he hasn't set a central widget for his MainWindow class. I don't know what has been done in Qt Designer, or whether it creates a central widget by default. If the code ui->centralWidget->setLayout() works without crashing, then maybe it has (or it points to uninitialized memory).

    So, modify the code by creating a plain old QWidget as a child of the MainWindow in the MainWindow constructor, set the layout on that widget, and then set that widget as the central widget for the MainWindow:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. // Other code as posted, except for line 30
    6. // ... at the end of the main window constructor
    7.  
    8. QWidget * pCentralWIdget = new QWidget( this );
    9. pCentralWidget->setLayout( vlayout );
    10. setCentralWidget( pCentralWidget );
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)

    Hey d_stranz,

    yea that's kind of what I wanted however using that method instead of "ui->centralwidget->setlayout" crashes the program with

    QLayout: Cannot add null widget to QVBoxLayout/
    QLayout: Cannot add null widget to QVBoxLayout/
    The program has unexpectedly finished.
    Have tried custom widgets now they are ok but I just really don't like the implementation especially considering that really these are as far from a "custom widget" as you can get, as I just need to create two Qwidgets inside another widget (with that widget being on the form, so it's easier creating the layout).

    If I don't want to use custom widgets how would I go about this? So far i've created a widget on the ui form called "widget" if I want to insert the two "Qwidgets" inside this am I supposed to be just using "setparent" ? or do i need to include anything else as when I do this using set parent the "widget" is very small and doesn't fill the screen.

    If someone could just show me a simple bit of code that creates a widget on the form (don't need to see this bit). Then makes this widget the parent of the two Qwidgets so that the two fill the whole of "widget", that would be great.
    Last edited by Ion; 19th September 2014 at 10:25.

  9. #9
    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: Qt Designer how to insert a QWidget into a form item(widget, frame ,etc)

    Qt Code:
    1. QVBoxLayout *layout = new QVBoxLayout(ui->widget); // or whatever your container is called
    2.  
    3. QWidget *widget1 = new ....;
    4. layout->addWidget(widget1);
    5. QWidget *widget2 = new ....;
    6. layout->addWidget(widget2);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Changing name of a Widget/Dialog (Designer form with Class)
    By alizadeh91 in forum Qt Programming
    Replies: 2
    Last Post: 15th March 2013, 20:46
  2. Replies: 0
    Last Post: 19th September 2011, 16:10
  3. Insert custom widget in the qt designer list box
    By eg3gg in forum Qt Programming
    Replies: 12
    Last Post: 6th September 2010, 09:06
  4. Replies: 0
    Last Post: 8th July 2009, 11:01
  5. Replies: 4
    Last Post: 6th February 2006, 14:30

Tags for this Thread

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.