Results 1 to 5 of 5

Thread: Adding Ui created widget into manually created main widget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2013
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Adding Ui created widget into manually created main widget

    I have a main widget derived from QWidget which I designed by manually coding. I was wondering if there is a way to add QtDesigner widget (ui) class to the main widget using addWidget function, or some other means. When I try to do addWidget in the MyMainWindow() function the program compiles fine, but the ui widget does not display. Thanks.


    class MyMainWindow: public QWidget
    {
    Q_OBJECT
    public:
    int count;
    MyCurve *c;
    inputs *in; //ui designed widget
    matrixVector A;
    QHBoxLayout *layout;
    QLineEdit *value;
    QLineEdit *g1;
    QLineEdit *g2;

    MyMainWindow();
    QMenu *fileMenu;
    QMenuBar *menuBar;
    QAction *openAction;
    QAction *saveAction;


    };

  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: Adding Ui created widget into manually created main widget

    The class generated from the .ui file is not a widget itself, it contains setup code for a widget's content.

    So what you do is create a QWidget subclass, have a member of the type of the generated class, and call its setupUi( this ) in the widget subclass' constructor.

    As a very, very, dirty hack you could also just instantiate a QWidget instance and call the UI class setupUi method passing that widget instance.

    Cheers,
    _

  3. #3
    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: Adding Ui created widget into manually created main widget

    Designer created widgets are regular widgets that adhere to every rule code-created widgets adhere to. Which means you can use it any way you want. Just remember that what Designer creates is not really a widget, but rather a class that needs to be deployed on a widget by calling setupUi().
    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.


  4. #4
    Join Date
    Mar 2013
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adding Ui created widget into manually created main widget

    Thanks for the replies. I do have the setupUi(this) in the constructor.

    inputs::inputs(QWidget *parent) :QWidget(parent), ui(new Ui::inputs)
    {
    ui->setupUi(this);
    }


    In the mainWidget constructor , I do
    in=new inputs(this);
    QHBoxLayout *lh= new QHBoxLayout;
    lv->addWidget(logo);
    lh->addWidget(in);


    But nothing shows on the screen. Is there something else needed?
    Thanks.

  5. #5
    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: Adding Ui created widget into manually created main widget

    You need to tell the layout what it is supposed to operate on (e.g. pass "this" to its constructor).
    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. use value from dynamically created widget in a slot
    By Cyrebo in forum Qt Programming
    Replies: 55
    Last Post: 23rd April 2013, 15:51
  2. Use python widget created with PyQt4 in Qt applications
    By alizadeh91 in forum Qt Programming
    Replies: 3
    Last Post: 27th November 2012, 22:33
  3. Subclassing a widget created in Qt Designer
    By dmginc in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2011, 07:30
  4. Replies: 2
    Last Post: 20th September 2009, 03:52
  5. Drawing on a widget created via QDesigner
    By jean in forum Qt Programming
    Replies: 5
    Last Post: 15th January 2009, 14:15

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.