Results 1 to 5 of 5

Thread: Stop multiple appearances of a widget in the same window

  1. #1
    Join Date
    Feb 2017
    Posts
    8
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Stop multiple appearances of a widget in the same window

    Hello,
    I have a vertical layout (called center) & 3 buttons (aide,info,prom).
    for each button clicked i want to add a specific widget into the vertical layout.
    the problem is everytime a button is clicked it specific wigdet will apear if i click another botton or the same button again it will add another wigdet next to the previous one.
    How can I stop that from happening.
    Here what I have so far:
    Qt Code:
    1. ...
    2. void employe::on_aide_clicked()
    3. {
    4. aide *a = new aide();
    5. ui->center->addWidget(a,Qt::AlignLeft);
    6. }
    7. void employe::on_info_clicked()
    8. {
    9. empInfo *i = new empInfo();
    10. ui->center->addWidget(i,Qt::AlignLeft);
    11. }
    12.  
    13. void employe::on_prom_clicked()
    14. {
    15. demPro *d = new demPro();
    16. ui->center->addWidget(d,Qt::AlignLeft);
    17. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Stop multiple appearances of a widget in the same window

    Well, that's exactly what your code is telling it to do - each time you click any of the three buttons, it will add -another- instance of the same widget to the layout.

    What do you really want to do? Add the widget once, the first time the button is clicked and never again? Destroy the old widget (if any) when the button is clicked and then add a new one? Or something else?

    Write down (in words) what you want to happen, then try to write the code to do that. Report back here on youor results, but not before you try that.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Feb 2017
    Posts
    8
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Stop multiple appearances of a widget in the same window

    For each button clicked I want to add one & only one widget. If another botton is clicked remove the previous one & add the new one to the layout.
    The layout can have just one widget.
    here what I tried:
    I declared all the widgets in the header file
    Qt Code:
    1. void employe::on_aide_clicked(){
    2. if(!ui->center->isEmpty()){
    3. ui->center->removeWidget(i);
    4. ui->center->removeWidget(d);
    5. }
    6. ui->center->addWidget(a,Qt::AlignLeft);
    7. }
    To copy to clipboard, switch view to plain text mode 
    & then did the same thing for the rest buttons
    It works but since I'm a newbie I don't know if it's the right thing to do or if it's just a messy solution.

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    514
    Thanks
    12
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Stop multiple appearances of a widget in the same window

    Hi, if you only want to display one of the three widgets at a time, maybe QStackedWidget is what you are looking for.

    Ginsengelf

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Stop multiple appearances of a widget in the same window

    maybe QStackedWidget is what you are looking for.
    Yes. Add the QStackedWidget to your vertical layout, then add the three sub-widgets as pages in the stack. Assign a stack page index (0, 1, or 2) to each of your buttons. So when the button for "a" is clicked, you set the current stack page to 0, for example. If you want a situation where -none- of the three sub-widgets is visible, then just add a plain QWidget instance as one of the pages.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 2
    Last Post: 27th March 2012, 14:33
  2. Replies: 12
    Last Post: 16th September 2011, 23:48
  3. Multiple appearances in a single ui file
    By zgulser in forum Qt Tools
    Replies: 1
    Last Post: 5th June 2009, 19:12
  4. Stop window moving when clicking LMB
    By steg90 in forum Qt Programming
    Replies: 11
    Last Post: 11th June 2007, 10:59
  5. Replies: 21
    Last Post: 13th March 2006, 14:05

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.