Results 1 to 3 of 3

Thread: Best way to swap images within main U?

  1. #1
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Best way to swap images within main U?

    I am about to develop an application that will let the user navigate through multiple screens. While each screen has its own set of options for the user, they all share a few common elements (a "Back" button, a "Cancel" button, etc.). I think I should be able to make one "base" UI with these common elements scattered around the edges and then just swap out the central part of the UI to give the user the new options. This way all of the common elements stay in the same position no matter where the user in in their navigation.

    My question is this: What is the best approach for doing this? I have done some investigating and looked at things like QGraphicsView and how to use multiple UI files for one application but it is not clear to me what the best approach is. It seems like this should be fairly straightforward but I would like to avoid going down blind alleys.

    I am using the Qt 5 plug-in with MSVC on a Windows machine.

    Thanks.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Best way to swap images within main U?

    - Design a main ui in the Qt Designer, add the navigation buttons and QStackedWidget in the center.
    - Connect the signal of push buttons to change the active widget (page) in the QStackedWidget.
    - You can create widget using Qt Designer and add them to the QStackedWidget individually.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    cmp1104 (15th October 2013)

  4. #3
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Best way to swap images within main U?

    Thank Santosh, this looks like exactly what I need.

    I tried to do a basic instance of this and got it to partially work. I can get a widget that exists within my main UI to be displayed in the stacked widget but I can not get a another UI I created as a widget to be displayed. Here is a sample of my test code:

    void MainWindow::ChangeWidgets(void)
    {
    QTime myTime; //Timer used to change images


    this->ui->testText->setText("Hello World"); //Setting some text just to show something happening

    cMyChild myChild(this->ui->myStackedWidget); //Instance of the class that represents the widget UI.

    this->ui->myStackedWidget->addWidget(&myChild); //Adding ui to the stacked widget
    this->ui->myStackedWidget->addWidget(this->ui->aWidget); //Adding a widget that already exists on the main UI
    this->ui->myStackedWidget->setCurrentIndex(0); //Setting the index of the stacked widget to the UI widget
    this->ui->myStackedWidget->show(); //Make sure that the stacked widget is being displayed

    myTime.start();

    while(myTime.elapsed() < 3000) //Wait 3 seconds
    {
    ;;
    }
    this->ui->myStackedWidget->setCurrentIndex(1); //Change the displayed widget to the one that exists on the main UI

    }

    When the index of the stacked widget is changed to 1 the widget that exists on the main UI gets displayed in the stacked widget. I am not sure why the myChild widget is not displayed. The constructor for myChild is this:

    cMyChild::cMyChild(QWidget *parent) : QWidget(parent), ui(new UI::cMyChild)
    {
    this->ui->setupUi(parent);
    this->show;
    }

    I am sure I am missing something obvious but I am not sure what.

    Thanks.


    Added after 30 minutes:


    Okay, I think I figured out what I was doing wrong. Two things:
    1. The index for the stacked widget is not zero basec.
    2. I changed how I was adding my UI widget to this->ui->myStackedWidget->addWidget(new cMyChild());

    It now appears to work.
    Last edited by cmp1104; 15th October 2013 at 15:27.

Similar Threads

  1. How to Swap the tabs(similar to Google chrome)
    By GT Development in forum Qt Programming
    Replies: 3
    Last Post: 7th October 2011, 15:31
  2. Swap on drag and drop
    By binaural in forum Newbie
    Replies: 0
    Last Post: 8th April 2010, 20:23
  3. swap cell widgets on QTableWidget
    By ulmly in forum Qt Programming
    Replies: 9
    Last Post: 5th March 2009, 11:18
  4. swap headers in QTableView
    By dimixan in forum Qt Programming
    Replies: 2
    Last Post: 26th May 2008, 21:44
  5. QTreeWidgetItem swap or move up one level problem
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2006, 19:34

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.