Results 1 to 3 of 3

Thread: Replece one widget for another in QGridLayout

  1. #1
    Join Date
    Jul 2015
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Windows

    Default Replece one widget for another in QGridLayout

    Hi!

    I've a two buttons width display the form. Fieleds in form depend from selected button:
    Qt Code:
    1. QWidget *base = new QWidget();
    2. layout = new QGridLayout(base);
    3.  
    4. LeNrEwidencyjny = new QLineEdit();
    5. LeNrRejestracyjny = new QLineEdit();
    6. LeNrTelefonu = new QLineEdit();
    7. GbPodstawoweInformacje = new QGroupBox();
    8.  
    9. QPushButton *BtnTypWlasny = new QPushButton(QIcon::QIcon(":/new/prefix1/user_business.png"),"pojazd własny");
    10. BtnTypWlasny->setFixedHeight(80);
    11. connect(BtnTypWlasny, SIGNAL(clicked()), this, SLOT(slotTypWlasny()));
    12.  
    13. QPushButton *BtnTypPrzewoznika = new QPushButton(QIcon::QIcon(":/new/prefix1/delivery.png"),"pojazd przewoźnika");
    14. BtnTypPrzewoznika->setFixedHeight(80);
    15. connect(BtnTypPrzewoznika, SIGNAL(clicked()), this, SLOT(slotTypPrzewoznika()));
    16.  
    17. layout->addWidget(BtnTypWlasny,0,0,1,1,Qt::AlignTop);
    18. layout->addWidget(BtnTypPrzewoznika,0,1,1,1,Qt::AlignTop);
    19.  
    20. base->show();
    21. this->setCentralWidget(base);
    To copy to clipboard, switch view to plain text mode 

    slotTypWlasny:
    Qt Code:
    1. void FormNowaCiezarowka_add::slotTypWlasny(){
    2. layoutTMP = new QGridLayout();
    3. GbPodstawoweInformacje = new QGroupBox("pojazd własny");
    4.  
    5. layoutTMP->addWidget(new QLabel("nr ewidencyjny1: "),0,0,1,1,Qt::AlignTop);
    6. layoutTMP->addWidget(LeNrEwidencyjny,0,1,1,1,Qt::AlignTop);
    7.  
    8. layoutTMP->addWidget(new QLabel("nr rejestracyjny1: "),1,0,1,1,Qt::AlignTop);
    9. layoutTMP->addWidget(LeNrRejestracyjny,1,1,1,1,Qt::AlignTop);
    10.  
    11. layoutTMP->addWidget(new QLabel("nr telefonu1: "),2,0,1,1,Qt::AlignTop);
    12. layoutTMP->addWidget(LeNrTelefonu,2,1,1,1,Qt::AlignTop);
    13.  
    14. GbPodstawoweInformacje->setLayout(layoutTMP);
    15. layout->addWidget(GbPodstawoweInformacje,1,0,1,2,Qt::AlignTop);
    16. }
    To copy to clipboard, switch view to plain text mode 

    and the slotTypPrzewoznika:
    Qt Code:
    1. void FormNowaCiezarowka_add::slotTypPrzewoznika(){
    2. layoutTMP = new QGridLayout();
    3. GbPodstawoweInformacje = new QGroupBox("pojazd przewoźnika");
    4.  
    5. layoutTMP->addWidget(new QLabel("nr ewidencyjny2: "),0,0,1,1,Qt::AlignTop);
    6. layoutTMP->addWidget(LeNrEwidencyjny,0,1,1,1,Qt::AlignTop);
    7.  
    8. layoutTMP->addWidget(new QLabel("nr rejestracyjny2: "),1,0,1,1,Qt::AlignTop);
    9. layoutTMP->addWidget(LeNrRejestracyjny,1,1,1,1,Qt::AlignTop);
    10.  
    11. GbPodstawoweInformacje->setLayout(layoutTMP);
    12. layout->addWidget(GbPodstawoweInformacje,1,0,1,2,Qt::AlignTop);
    13. }
    To copy to clipboard, switch view to plain text mode 
    whet i've pushed BtnTypWlasny and next push the BtnTypPrzewoznika i've got:
    2.png


    I can't replece one widget by another :
    Qt Code:
    1. layout->addWidget(GbPodstawoweInformacje,1,0,1,2,Qt::AlignTop);
    To copy to clipboard, switch view to plain text mode 

    I've tried for example:
    Qt Code:
    1. layout->removeWidget(layout->itemAtPosition(1, 0)->widget());
    2. QWidget* widget0 = layout->itemAt(2)->widget();
    3. delete widget0;
    To copy to clipboard, switch view to plain text mode 

    but it doesn't work

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Replece one widget for another in QGridLayout

    What if you just delete the respective other groupbox widget?

    Also you should probably check if the widget you are creating already exists, e.g. if the same button is clicked twice.

    But what you should really consider is to put a QStackedWidget into cell 1,0 and use those two groupboxes as it pages.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Replece one widget for another in QGridLayout

    layout->removeWidget(layout->itemAtPosition(1, 0)->widget());
    Doesn't this change the indexes of the remaining grid items?

    QWidget* widget0 = layout->itemAt(2)->widget();
    And doesn't this need a column index?

Similar Threads

  1. Coordinates of a widget inside QGridLayout
    By kolqhoz in forum Newbie
    Replies: 1
    Last Post: 30th March 2014, 06:31
  2. Replies: 6
    Last Post: 28th June 2013, 01:38
  3. QGridLayout: force all widget to the same size
    By Spooky in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2011, 13:58
  4. Delete a QGridLayout and New QGridLayout at runtime
    By sabeesh in forum Qt Programming
    Replies: 1
    Last Post: 5th November 2007, 13:01
  5. Adjusting Widget size in a QGridLayout
    By Max Yaffe in forum Qt Programming
    Replies: 1
    Last Post: 30th July 2007, 22:03

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.