Results 1 to 10 of 10

Thread: How to add widgets when program is already running?

  1. #1
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to add widgets when program is already running?

    I'm trying to do some basic game that's about adding, using and deleting widgets. My problem is that I can only create widgets at program startup, if I'd like to restart the game (without closing application), the newly created widgets aren't appearing.

    The QGridLayout solves that problem but creates another, manages widget positions itself, and I don't want that.

    So is there anyway to create widget when program is already running (without need of adding widget to layout)?

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to add widgets when program is already running?

    if you are using a layout then you have to insert the widgets in the layout...
    otherwise you can just call
    widget = new QWidget(this)
    widget->move();
    any time to add a widget.. i dont understand whats the big deal in this?

  3. #3
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add widgets when program is already running?

    Indeed, you don't understand.

    Qt Code:
    1. void UserInterface::addButtons()
    2. {
    3. for (quint8 x = 1; x <= 10; x++) {
    4. for (quint8 y = 1; y <= 10; y++) {
    5. QPushButton* button = new QPushButton(this);
    6. button->setGeometry(x * 30, y * 30, 30, 30);
    7. buttonList.push_back(button);
    8. }
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    If I call this function from constructor it's all good, everything works. But if I call it from a slot function (at random time) then widgets doesn't appear on the main window. Do you see my problem now?

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add widgets when program is already running?

    just query ...
    "buttonList" did u declare it globally .. how u declare it ..
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add widgets when program is already running?

    Qt Code:
    1. QList <QPushButton*> buttonList;
    To copy to clipboard, switch view to plain text mode 
    It's just to hold the address of the widgets, it's not important in that example.

  6. #6
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add widgets when program is already running?

    so u are not adding the widget to base class QWidget layout .. try to add that widget u are storing in QList to your base ..
    "Behind every great fortune lies a crime" - Balzac

  7. #7
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add widgets when program is already running?

    You don't understand either.

    The widgets are created when I call that function from constructor of the class.
    The widgets aren't created when I call that function via slot function.

    The buttonList is not important at all. It just holds the address of the widgets so later I can access them and do whatever I want to do with them. I could simply leave that line alone but how would I delete some widgets later on?

  8. #8
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add widgets when program is already running?

    button->show() ?

    In the costructor you don't need becouse the window show is propagated for every childreen...

    Works?

  9. The following user says thank you to PaceyIV for this useful post:

    RSX (23rd August 2009)

  10. #9
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add widgets when program is already running?

    Yes, button->show() solves my problem, thanks.

  11. #10
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to add widgets when program is already running?

    If you want to replace the old buttons with new ones, remember to delete any previous buttons you are not using anymore. Otherwise you will have memory leaks (the buttons will take memory that is allocated but can't be accesed and is useless). The best solution would be to try to reuse the buttons or keep a list of pointers to the buttons and use qDeleteAll(). But if all the buttons are children of your main widget and you don't use any other buttons in it, you may use this:

    Qt Code:
    1. foreach(QObject* child, children()) {
    2. if(child->metaObject()->className() == "QPushButton")
    3. child->deleteLater();
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to check if a program is running?
    By raphaelf in forum Newbie
    Replies: 16
    Last Post: 30th April 2010, 15:59
  2. Help me how to know the program is running
    By vql in forum Qt Programming
    Replies: 1
    Last Post: 21st March 2008, 12:43
  3. Detect platform where my QT4 program is running on
    By the_bis in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2007, 12:01
  4. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19
  5. Replies: 1
    Last Post: 17th May 2006, 00:23

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.