Results 1 to 5 of 5

Thread: Create And Display QWidget in code

  1. #1
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Create And Display QWidget in code

    Good day All.
    Ive been using qt creator to do all my qt application development and therefore using ui pages in creator for design layout.
    How ever i have the need to create a widget or dialog that i would like to do solely in code no ui page.

    This is what i have done so far.

    I created a new C++ class not qt class so it doesnt include the ui file.
    the file is named "frm_maintenance_all"
    I then included the file and open it so in my mainwindow.cpp

    Qt Code:
    1. void MainWindow::openMaintenance()
    2. {
    3. frm_maintenance_all *newform = new frm_maintenance_all;
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    Then my header is

    Qt Code:
    1. #ifndef FRM_MAINTENANCE_ALL_H
    2. #define FRM_MAINTENANCE_ALL_H
    3.  
    4. #include <QWidget>
    5. #include <QDialog>
    6. #include <QtGui>
    7. #include <QApplication>
    8.  
    9. class frm_maintenance_all : public QWidget
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit frm_maintenance_all(QWidget *parent = 0);
    14.  
    15. signals:
    16.  
    17. public slots:
    18.  
    19. private slots:
    20. void frm_create(QString tablename);
    21.  
    22. };
    23.  
    24. #endif // FRM_MAINTENANCE_ALL_H
    To copy to clipboard, switch view to plain text mode 

    and my cpp is

    Qt Code:
    1. #include "frm_maintenance_all.h"
    2.  
    3. frm_maintenance_all::frm_maintenance_all(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. frm_create("test");
    7.  
    8. }
    9.  
    10. void frm_maintenance_all::frm_create(QString tablename)
    11. {
    12.  
    13.  
    14. QWidget *frmnew = new QWidget;
    15. QVBoxLayout *myhoz = new QVBoxLayout;
    16.  
    17. QLineEdit *myline = new QLineEdit;
    18. QLineEdit *myline1 = new QLineEdit;
    19.  
    20. myhoz->addWidget(myline);
    21. myhoz->addWidget(myline1);
    22.  
    23. //attempt here to add myhoz to frmnew via frmnew->addLayout does not work
    24.  
    25. frmnew->show();
    26. frmnew->setWindowTitle("Maintenance Test");
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

    you will see in the coment above i try to add the layout created to the frm however it does not give me the option to add widget or addlayout.
    Is there something im doing wrong.

    Or is there a better way to do this.

    Regards

  2. #2
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Create And Display QWidget in code

    Try using setLayout instead of addLayout.

  3. #3
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Create And Display QWidget in code

    Thanks that worked Perfectly, such a stupid little thing.

    Regards

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Create And Display QWidget in code

    what is the point in that class? why have it inherit from QWidget?

    It will never be anything other than a blank window as it is. and the only thing it *does* do is make another widget and show it.

    It would be more conventional to do this
    Qt Code:
    1. #include "frm_maintenance_all.h"
    2.  
    3. frm_maintenance_all::frm_maintenance_all(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. frm_create("test");
    7.  
    8. }
    9.  
    10. void frm_maintenance_all::frm_create(QString tablename)
    11. {
    12. QVBoxLayout *myhoz = new QVBoxLayout;
    13.  
    14. QLineEdit *myline = new QLineEdit;
    15. QLineEdit *myline1 = new QLineEdit;
    16.  
    17. myhoz->addWidget(myline);
    18. myhoz->addWidget(myline1);
    19.  
    20. //attempt here to add myhoz to frmnew via frmnew->addLayout does not work
    21.  
    22. setLayout(myhoz);
    23. setWindowTitle("Maintenance Test");
    24. show();
    25. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Create And Display QWidget in code

    Thanks that works great.

Similar Threads

  1. How to create CWnd from QWidget?
    By vudvpro in forum Qt Programming
    Replies: 5
    Last Post: 10th May 2011, 11:01
  2. How to dynamicly create a DSN by using Qt code?
    By bangqianchen in forum Qt Programming
    Replies: 1
    Last Post: 29th December 2009, 23:15
  3. Example of QWidget::create required
    By andyp in forum Qt Programming
    Replies: 0
    Last Post: 21st October 2009, 11:09
  4. Cannot create a QWidget when no GUI is being used
    By Teerayoot in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2007, 14:29
  5. Replies: 4
    Last Post: 24th April 2007, 13:18

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.