Results 1 to 8 of 8

Thread: dinamically create widgets

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2008
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default dinamically create widgets

    Here is my scenario :

    1 . I have a MainWindow
    2. I have a QPushButton on it
    3. When i press it, i want to dinamically create N PushButtons.
    4. When i press any of those buttons, i want something specific to happen ( like a slot with the name of the button that was pressed in a parameter or smth like that ) .

    How can i make this ?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: dinamically create widgets

    What did you try so far? Did you create a slot? Did you establish a signal-slot connection between the button and the slot?
    J-P Nurmi

  3. #3
    Join Date
    Dec 2008
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dinamically create widgets

    Here is my code so far.

    app.h
    Qt Code:
    1. #ifndef APP_H
    2. #define APP_H
    3.  
    4. #include "ui_gui.h"
    5.  
    6. class App1:public QMainWindow,private Ui::Form1
    7. {
    8. Q_OBJECT
    9. public:
    10. App1();
    11. QPushButton *Button2;
    12. private slots:
    13. void Button1Clicked();
    14. };
    15.  
    16. #endif
    To copy to clipboard, switch view to plain text mode 

    app.cpp

    Qt Code:
    1. #include <QtGui>
    2. #include "app.h"
    3.  
    4. App1::App1()
    5. {
    6. connect(Button1,SIGNAL(clicked()),this,SLOT(Button1Clicked()));
    7. }
    8.  
    9.  
    10. void App1::Button1Clicked()
    11. {
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    What should i do next ?

    I already tried smth like this:

    Qt Code:
    1. void App1::Button1Clicked()
    2. {
    3. QPushButton *btn = new QPushButton();
    4. // etc . set it's properties
    5. }
    To copy to clipboard, switch view to plain text mode 

    But it doesn't appear on the main window .

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: dinamically create widgets

    try this
    Qt Code:
    1. void App1::Button1Clicked()
    2. {
    3. QPushButton *btn = new QPushButton();
    4. setCentralWidget(btn);
    5. // etc . set it's properties
    6. }
    To copy to clipboard, switch view to plain text mode 
    but it's just example, in general you need to create a widget put layout on it, add another widgets and add it to created layout and then set that (first) widget as central widget of mainwindow and everything will work perfectlly.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Dec 2008
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dinamically create widgets

    i did as you said but the new created button occupies all the mainwindow

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: dinamically create widgets

    Quote Originally Posted by msmihai View Post
    i did as you said but the new created button occupies all the mainwindow
    Yes that's what it does, spirit just gave it as an example. Please read through the documentation of Layout Classes.
    J-P Nurmi

  7. #7
    Join Date
    Dec 2008
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: dinamically create widgets

    in my ui_gui.h file ( automatically generated ) , i have an object called centralwidget.
    i think it has something to do with it. could someone please tell me what should i do ?
    it's not in my nature to directly ask for code but i just can't figure it out
    please help me

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: dinamically create widgets

    on central widget you can add layouts and another widgets.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. How to create Custom Slot upon widgets
    By ashukla in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2007, 14:07
  2. How to create a movable border between two widgets?
    By Teuniz in forum Qt Programming
    Replies: 2
    Last Post: 16th March 2007, 08:45
  3. create custom widgets
    By nimmyj in forum General Discussion
    Replies: 1
    Last Post: 20th November 2006, 08:24
  4. How to create pop up widgets in designer.
    By gsQT4 in forum Qt Tools
    Replies: 1
    Last Post: 25th May 2006, 16:40
  5. How to movable dock widgets?
    By becond in forum Qt Tools
    Replies: 3
    Last Post: 21st February 2006, 19:57

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
  •  
Qt is a trademark of The Qt Company.