Results 1 to 3 of 3

Thread: Add a custom widget in code to a MainWindow

  1. #1
    Join Date
    Nov 2009
    Posts
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Add a custom widget in code to a MainWindow

    Hy there,

    i am new to Qt and Creator and have the following problem.

    I whant to add an OpenGL QGLWidget to a QGuiApplication that i have created with the new project wizard. Then I add a new QTWidget (QT Designer Form Class) also over the wizard and change

    Qt Code:
    1. class gl : public QWidget {
    To copy to clipboard, switch view to plain text mode 

    to

    Qt Code:
    1. class gl : public QGLWidget {
    To copy to clipboard, switch view to plain text mode 

    in .h and .cpp

    i alos add

    Qt Code:
    1. #include "QGLWidget"
    To copy to clipboard, switch view to plain text mode 

    in the .h file

    Now i need to create a new instance of my class here and connect it to the ui

    Qt Code:
    1. GLTest::GLTest(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::GLTest)
    3. {
    4. // have to add it here
    5. ui->setupUi(this);
    6. // or here and how ?
    7. }
    To copy to clipboard, switch view to plain text mode 

    But i do not get it to work.
    Can you give me an smale example?
    I know that i have to add it to the CentraWidget.

  2. #2
    Join Date
    Feb 2008
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add a custom widget in code to a MainWindow

    Hi Steff,

    You have to create an instance of gl class like the following:

    Qt Code:
    1. gl *g = new gl();
    To copy to clipboard, switch view to plain text mode 

    where gl is the class that I've seen from your code.

    The complete code from your question will be as follows:

    Qt Code:
    1. GLTest::GLTest(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::GLTest)
    3. {
    4.  
    5. ui->setupUi(this);
    6. gl *g = new gl();
    7.  
    8. setCentralWidget(g); // where g is your instance of gl class.
    9. }
    To copy to clipboard, switch view to plain text mode 


    If you have any further questions, please don't hesitate to ask

  3. #3
    Join Date
    Nov 2009
    Posts
    2
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Add a custom widget in code to a MainWindow

    Thank you for the fast reply.

    Becaus of the defination of the Widget is:

    Qt Code:
    1. class gl : public QGLWidget {
    2. Q_OBJECT
    3. public:
    4. gl(QWidget *parent = 0);
    5. ~gl();
    6.  
    7. protected:
    8. void changeEvent(QEvent *e);
    9.  
    10. private:
    11. Ui::gl *m_ui;
    12. };
    To copy to clipboard, switch view to plain text mode 

    I thought i must add the parent element to the creation call.

    Qt Code:
    1. gl *g = new gl(ui);
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. gl *g = new gl(this);
    To copy to clipboard, switch view to plain text mode 

    But i din not try just to call it without this

    Thank you

Similar Threads

  1. Replies: 2
    Last Post: 18th November 2010, 22:50
  2. Replies: 5
    Last Post: 25th May 2008, 08:22
  3. resizing events of a custom widget in a layout
    By Rooster in forum Qt Programming
    Replies: 7
    Last Post: 16th February 2008, 10:52
  4. Replies: 3
    Last Post: 12th April 2006, 08:20
  5. Replies: 4
    Last Post: 6th February 2006, 14:30

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.