Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add different widgets in same position inQGridLayout
Forum Updated to NodeBB v4.3 + New Features

How to add different widgets in same position inQGridLayout

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GAURAV PANT
    wrote on last edited by
    #1

    Hi,
    I am making a Qt widget application having following wigets: button1, button2, label1, label2.
    I have a QGridLayout having two rows.
    In row 1 I have button1 and button2. Now if button1 is clicked then label1 is displayed in row2 and if button2 is clicked label2 is displayed in row2.

    Here is my code:
    @
    {
    QPushButton *btn1=new QPushButton("click1");
    connect(btn, SIGNAL(clicked()), this, SLOT(disp1()));

    QPushButton *btn2=new QPushButton("click2");
    connect(btn1, SIGNAL(clicked()), this, SLOT(disp2()));
    
    gd=new QGridLayout();
    gd->addWidget(btn1,0,0);
    gd->addWidget(btn2,0,1);
    
    parent->setLayout(gd);
    

    }

    void MainWindow::disp1()
    {

    QLabel *lb1=new QLabel("label1");   
    gd->addWidget(lb1,1,0);
    

    }

    void MainWindow::disp2()
    {

       QLabel *lb2=new QLabel("label2"); 
       gd->addWidget(lb2,1,0);
    

    }
    @

    Whats happening is label1 and label2 are getting overlapped but i want to display only one at a time.
    I have also tried adding label1 and label2 to QFrame and then using frame1->hide() and frame2->hide() but some flickering of screen is there and a warnig is also displayed "Attempting to add QLayout to QFrame , which already has a layout"

    Kindly help.

    Thanks

    [edit: code tags added, koahnig]

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      "Please use code wrapping tags.":http://qt-project.org/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01

      I see two different possibilities either using "replaceWidget":http://qt-project.org/doc/qt-5/qlayout.html#replaceWidget or use "setVisible(false)":http://qt-project.org/doc/qt-5/qwidget.html#visible-prop for the widget, you do not want to see and vice versa. This can be used for QLabel and QPushButton and lot others.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved