Results 1 to 4 of 4

Thread: "Change widget class on button click" problem

  1. #1
    Join Date
    May 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default "Change widget class on button click" problem

    Hi, I'm making qt gui application, and in mainwindow I have QWidget, which should become one of my custom widgets..

    here's a code

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent)
    3. {
    4. QWidget *tempWidget = new QWidget(this);
    5. setMinimumSize(800,600);
    6. QFormLayout *mainLayout=new QFormLayout();
    7. QVBoxLayout *btnsLayout=new QVBoxLayout;
    8. btns=new QGroupBox;
    9. widget=new QWidget;
    10. widget->setStyleSheet("height: 400px; width: 500px;");
    11. onlineGameButton=new QPushButton("ONLINE GAME");
    12. connect(onlineGameButton, SIGNAL(clicked()), this, SLOT(newOnlineGame()));
    13. offlineGameButton=new QPushButton("OFFLINE GAME");
    14. simulateGameButton=new QPushButton("SIMULATE GAME");
    15. optionsButton=new QPushButton("OPTIONS");
    16. exitButton=new QPushButton("EXIT");
    17. connect(exitButton, SIGNAL(clicked()), this, SLOT(exitProgram()));
    18. btnsLayout->addWidget(onlineGameButton);
    19. btnsLayout->addWidget(offlineGameButton);
    20. btnsLayout->addWidget(simulateGameButton);
    21. btnsLayout->addWidget(optionsButton);
    22. btnsLayout->addWidget(exitButton);
    23. btnsLayout->addStretch();
    24. btns->setLayout(btnsLayout);
    25. mainLayout->addRow(btns, widget);
    26. ...
    27. tempWidget->setLayout(mainLayout);
    28. setCentralWidget(tempWidget);
    29. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::newOnlineGame()
    2. {
    3. widget=new GameWidgetView;
    4. }
    To copy to clipboard, switch view to plain text mode 

    i used debug to view if event is fired, and it is, but nothing happend, widget didn't become GameWidgetView...

    widget is defined as private member of MainWindow, so when I add it to centralwidget, when i change widget, it should be changed and "that widget" in centralwidget, too, right(because of reference)?

    sorry for my english

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: "Change widget class on button click" problem

    You only create a new widget and assign it to some variable. There is no "replacement" going on. If you want to replace one widget with another then either use QStackedWidget or delete the old widget and place the new one in a layout where the old widget was.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    utkozanenje (22nd May 2011)

  4. #3
    Join Date
    May 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "Change widget class on button click" problem

    great! thanks lot, it works.

    these are correction
    //first change stupid name widget to less stupid name _widget
    in mainwindow.h
    Qt Code:
    1. QStackedWidget *_widget;
    To copy to clipboard, switch view to plain text mode 

    in mainwindow.cpp
    Qt Code:
    1. _widget=new QStackedWidget;
    2. _widget->addWidget(new QWidget);
    3. _widget->addWidget(new GameWidgetView);
    4. _widget->setStyleSheet("height: 400px; width: 500px;");
    5. ...
    6. }
    7.  
    8. void MainWindow::newOnlineGame()
    9. {
    10. _widget->setCurrentIndex(1);
    11. }
    To copy to clipboard, switch view to plain text mode 

    this is exactle what i need because i have a lot of widget to put on this stack

  5. #4
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: "Change widget class on button click" problem

    There are several other ways you could do it. Simplest would be to parent the new widget with the old one and set the new widget's position to be (0,0) relative to the parent and its size to be the same as the parent. And, of course, don't forget to show() it.

Similar Threads

  1. Replies: 3
    Last Post: 8th December 2011, 20:21
  2. How to hide Close Button "X" on Widget ?
    By merry in forum Qt Programming
    Replies: 7
    Last Post: 3rd May 2010, 19:04
  3. Replies: 1
    Last Post: 3rd May 2010, 10:25
  4. Replies: 2
    Last Post: 25th August 2006, 12:35

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.