Results 1 to 2 of 2

Thread: Switch between models on click of a button

  1. #1
    Join Date
    Dec 2016
    Posts
    15
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Switch between models on click of a button

    Hi,

    I am currently set ModelA to a view. On clicking a button, I want to toggle between model B and model A in the same view. (The columns remain the same, the number of rows and the data is entirely different).
    Both models are of the QStandardItemModel type.

    Is there a better and faster way to have the switch other than using view->setmodel every time?


    I tried setting a fixed 'mainmodel', which is always set to the view. Then I tried assigning the mainmodel to modelb or modelA and tried to see if these changes reflect instantaneously.
    But this did not work, the view did not update on simple assignment.

    Can someone please guide me to how this is usually done?

    Thanks,
    Padma

  2. #2
    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: Switch between models on click of a button

    Is there a better and faster way to have the switch other than using view->setmodel every time?
    Not that I know of. If the contents of the two models are completely different, then the view needs to completely update itself no matter how you try to "trick" it.

    If the models are different, but their content does not change often, then instead of switching models on a single view, you can try using two views, one for each model, and simply switch the visibility of the views by using something like a QStackedWidget where you add each of the views as a widget in the stack. In this case, changing which model is displayed is as simple as calling QStackedWidget::setCurrentIndex(). Your code to do this could go something like this:

    Qt Code:
    1. void MyClass::onSwitchViewsClicked()
    2. {
    3. int currentView = mpStackedWidget->currentIndex();
    4. int nextView = (currentView + 1) % 2;
    5. mpStackedWidget->setCurrentIndex( nextView );
    6. }
    To copy to clipboard, switch view to plain text mode 

    Repainting after the switch will be instantaneous.
    <=== 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. Qpushbutton do something until click other button
    By isan in forum Qt Programming
    Replies: 10
    Last Post: 25th April 2016, 17:24
  2. How to get the selected row when click the button in it?
    By weixj2003ld in forum Qt Programming
    Replies: 7
    Last Post: 10th November 2011, 10:00
  3. How can I know which button click.
    By electronicboy in forum Qt Programming
    Replies: 10
    Last Post: 4th October 2009, 14:27
  4. button click in webview
    By mind_freak in forum Qt Programming
    Replies: 1
    Last Post: 29th September 2009, 13:48
  5. QPaintEvent on button click?
    By vishal.chauhan in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 08:44

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.