Results 1 to 4 of 4

Thread: Add custom Widget into window (not using Designer)

  1. #1
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Question Add custom Widget into window (not using Designer)

    I'm a total newbie, so don't be mean if this is total bogus. I want to write a pretty complex GUI, but I really don't want to write all my code into one single class. Does everything that's going to be in one window has to be in one class ? From what I already read I can create custom Widgets and add them into my code. But when I try, it doesn't work: The custom widget is always a seperate window. So I hope anybody can help me. I'm assuming I'm making a trivial mistake. I just can't find a tutorial for this since everybody just creates one custom widget and opens it from main as a new window. But I want to include my custom widget *into* the window.
    I made a real simple version of my problem. Just two groupBoxes each having a button which are supposed to be on top of each other within ONE window. The top button represents everything I want to add from myWidget, the bottom one stands for all the other stuff (propably other custom widgets). The problem is they are always in different windows. How can I get them into one? Please tell me why my thinking is wrong!
    So here goes nothing:

    #myWidget.cpp
    Qt Code:
    1. #include "mywidget.h"
    2.  
    3. #include <QGroupBox>
    4. #include <QHBoxLayout>
    5. #include <QPushButton>
    6.  
    7. myWidget::myWidget(QWidget *parent) : QWidget(parent)
    8. {
    9. QGroupBox *grInMyW = new QGroupBox("Group In myWidget");
    10. QHBoxLayout *layInMyW = new QHBoxLayout();
    11. QPushButton *butInMyW = new QPushButton("Button in myWidget");
    12.  
    13. layInMyW->addWidget(butInMyW);
    14. grInMyW->setLayout(layInMyW);
    15. grInMyW->setVisible(true);
    16. }
    To copy to clipboard, switch view to plain text mode 

    #main.cpp
    Qt Code:
    1. #include "mywidget.h"
    2.  
    3. #include <QApplication>
    4. #include <QGroupBox>
    5. #include <QVBoxLayout>
    6. #include <QPushButton>
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication a(argc, argv);
    11. QGroupBox *gr = new QGroupBox();
    12. QVBoxLayout *layout = new QVBoxLayout();
    13.  
    14. myWidget *myW = new myWidget;
    15. QPushButton* butInMain = new QPushButton("Button in Main");
    16.  
    17. layout->addWidget(myW);
    18. layout->addWidget(butInMain);
    19. gr->setLayout(layout);
    20. gr->show();
    21. return a.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    I don't know if I even need the GroupBoxes, but I read that for a custom widget i have to include some Layout, for its size to be clear. If there is an easier way, I'd really appreciate you telling me !
    But I'd be thankful for any answer I can get !!

  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: Add custom Widget into window (not using Designer)

    Quote Originally Posted by invisible_uli View Post
    I'm a total newbie, so don't be mean if this is total bogus.
    That's alright, we've all been beginners once.

    Does everything that's going to be in one window has to be in one class ?
    No, of course not.

    But when I try, it doesn't work: The custom widget is always a seperate window.
    A widget becomes a top-level window if it has no parent. You can set a parent in one of two ways - either by setting it explicitly (by passing a pointer to the parent to the constructor or using QWidget::setParent()) or by placing the widget inside a layout of another widget (this other widget becomes its parent widget then).

    I'm assuming I'm making a trivial mistake.
    Indeed. You just forgot to set a layout for the top-level widget and add all children to it.

    How can I get them into one?
    I'll give you a simpler example.

    Qt Code:
    1. #include <QtGui>
    2. int main(int argc, char **argv) {
    3. QApplication app(argc, argv);
    4. QWidget w; // top-level, no parent
    5. QPushButton *topButton = new QPushButton; // no parent thus top-level so far
    6. QPushButton *bottomButton = new QPushButton; // same as above
    7. QVBoxLayout *l = new QVBoxLayout(&w); // adding a layout to "w"
    8. l->addWidget(topButton); // sets a parent implicitly, becomes a child widget
    9. l->addWidget(bottomButton); // same as above
    10. w.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    If you comment lines 7-9, buttons will be separate windows as well.

    All that is left to say is that all widgets behave the same - built-in or custom ones so you can replace QWidget or QPushButton with any other widget class. You can consider my "w" widget a custom one as well.

    I don't know if I even need the GroupBoxes
    No, you don't.
    but I read that for a custom widget i have to include some Layout, for its size to be clear.
    Not really but that's close to being true
    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. The following user says thank you to wysota for this useful post:

    invisible_uli (12th January 2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Add custom Widget into window (not using Designer)

    Thanks so much, wysota ! I totally forgot to connect the Widgets in MyWidget to the parent.
    I cleaned it up now:

    myWidget.cpp
    Qt Code:
    1. #include "mywidget.h"
    2. #include <QPushButton>
    3.  
    4. myWidget::myWidget(QWidget *parent) : QWidget(parent)
    5. {
    6. QPushButton *butInMyW = new QPushButton("Button in myWidget");
    7. butInMyW->setParent(parent);
    8. }
    To copy to clipboard, switch view to plain text mode 

    It still didn't work with simply layout->addWidget(myW) in main.cpp. I guess it doesn't set a parent implicetly when using a custom Widget !? Because when using another Button instead of myWidget it works fine. But now I used an extra QWidget to be parent of myWidget:

    here's just a snippet of main.cpp
    Qt Code:
    1. QWidget helpWid;
    2.  
    3. myWidget *myW = new myWidget(&helpWid);
    4. //QPushButton *myW = new QPushButton("other Button");
    5.  
    6. layout->addWidget(&helpWid);
    To copy to clipboard, switch view to plain text mode 

    Or is there another way without using the extraWidget, did I make another mistake
    Well, I'm glad it works at least this way now. Thanks again wysota !!!

  5. #4
    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: Add custom Widget into window (not using Designer)

    You didn't set a layout for your "myWidget" widget, that's one thing. Another is that the parent of butInMyW should be an instance of "myWidget" (i.e. "this") and not its parent. Follow the same principle like I did in my main().
    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.


Similar Threads

  1. Custom widget using QT designer
    By GadiK in forum Qt Programming
    Replies: 10
    Last Post: 28th December 2009, 14:21
  2. Custom widget - Only works in Designer
    By Darhuuk in forum Qt Tools
    Replies: 6
    Last Post: 8th January 2008, 00:46
  3. Replies: 2
    Last Post: 12th July 2007, 09:55
  4. QT4: Custom widget for QT Designer
    By Michiel in forum Qt Tools
    Replies: 4
    Last Post: 4th May 2006, 13:35
  5. Custom widget + Qt Designer
    By chombium in forum Qt Tools
    Replies: 1
    Last Post: 12th April 2006, 20:33

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.