Results 1 to 10 of 10

Thread: Load new view (.ui) from the previous view

  1. #1
    Join Date
    Nov 2016
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Post Load new view (.ui) from the previous view

    Hi,

    I want to create an application whose content of the window will change after some time (like a time-lapse or timeout). E.g. I have created different .ui files for different content:

    a.ui

    b.ui

    First the main view "a.ui" is visible and after some time I want to "b.ui" to load into the same window. Any idea how to do this?

    (I tried to hiding one window and show another. But is there a better way of doing it where one view dissolves and the other one appears.)

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,056
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Load new view (.ui) from the previous view

    I am sure you understand that .ui files are compiled into your program and are not used at runtime, right?

    You can simulate fade in / fade out using the QWidget::windowOpacity() property and QPropertyAnimation. Animate the opacity for the widget you want to fade out to go from 1.0 to 0.0 and at the same time animate the opacity for the widget you want to fade in from 0.0 to 1.0. When you want to do a dissolve, set the opacity for the fade in widget to 0.0 and the fade out widget to 1.0, call QWidget::show() on both, then start the animations. You can connect the fade out widget's QWidget::hide() slot to the QAbstractAnimation::finished() signal to properly hide it once the animation ends.
    <=== 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.

  3. #3
    Join Date
    Nov 2016
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Load new view (.ui) from the previous view

    Thank you. Yes i understand the ui files are compiled since they are being built on QT designer. Sorry, I am very new to QT. Is the below idea right , can you please correct me. Now when i run the application , it crashes.
    Qt Code:
    1. void MainWindow::changeView(QWidget *parent){
    2.  
    3. //to load the second view after a time lapse
    4. //parent->windowOpacity()
    5. second = new secondView(this);
    6. QPropertyAnimation *animation = new QPropertyAnimation(parent, "windowOpacity()");
    7. QPropertyAnimation *animation2 = new QPropertyAnimation(second, "windowOpacity()");
    8. animation->setDuration(5000);
    9. animation2->setDuration(5000);
    10. second->show();
    11. animation->setStartValue(1.0);
    12. animation2->setStartValue(0.0);
    13. animation->setEndValue(0.0);
    14. animation2->setEndValue(1.0);
    15. parent->hide();
    16. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,056
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Load new view (.ui) from the previous view

    I think in lines 6 and 7 you want "windowOpacity", not "windowOpacity()". And it probably should be:

    Qt Code:
    1. QString( "windowOpacity" ).toLatin1();
    2. // or
    3. QString( "windowOpacity" ).toUtf8();
    To copy to clipboard, switch view to plain text mode 

    since the property name is a QByteArray type. I don't know which version is appropriate; you'll have to check.

    Each time you create the new "second" window, that's a memory leak. "second" should be a member variable of MainWindow, and you should create it once in the MainWindow constructor. You haven't given it a size. You should also set its opacity to 0 so that when it is first shown, it is invisible because it is totally transparent. You do not want to hide "parent" in line 15, because that makes it disappear immediately instead of fading out.

    As I said in my original post, connect parent's hide() slot to the "animation" finished() signal. This will cause it to actually be hidden when its opacity animation ends (at which point it is totally transparent).

    You will find the 5 seconds is much too long for this. 2 - 3 seconds will probably look better and lead to less impatience.

    You might also want to disable both widget when you start the dissolve so the user can't click on anything inside them. In the slot connected to the "animation2" finished() signal, you should enable "second".
    Last edited by d_stranz; 30th November 2016 at 22:46.
    <=== 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.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,056
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Load new view (.ui) from the previous view

    I am sure you understand that .ui files are compiled into your program and are not used at runtime, right?
    I was a bit wrong with this statement. See QUiLoader.
    <=== 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.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Load new view (.ui) from the previous view

    Quote Originally Posted by d_stranz View Post
    I was a bit wrong with this statement. See QUiLoader.
    Sure, but you were still quite right as that is almost never used.
    Basically only if the application supports some form of user scripting and needs a way for scripts to load UI.

    Cheers,
    _

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,056
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Load new view (.ui) from the previous view

    Agreed. I think it is a quite dangerous thing to put in the hands of a user who could make changes that destroy an app's GUI.
    <=== 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.

  8. #8
    Join Date
    Nov 2016
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Load new view (.ui) from the previous view

    Hi, Thanks for the replies both of you. Sorry, but I am still confused. Not sure how to use QAbstractAnimation:: finished () linking to the hiding of parent view.

    Qt Code:
    1. QPropertyAnimation *animation = new QPropertyAnimation(parent, QString("windowOpacity").toLatin1());//windowOpacity");
    2. QPropertyAnimation *animation2 = new QPropertyAnimation(second, QString("windowOpacity").toLatin1());//"windowOpacity");
    3. animation->setDuration(3000);
    4. animation2->setDuration(3000);
    5. animation2->setStartValue(0.0);
    6. ui->centralWidget->setEnabled(true);
    7. second->show();
    8. animation->setStartValue(1.0);
    9.  
    10. animation->setEndValue(0.0);
    11. animation2->setEndValue(1.0);
    To copy to clipboard, switch view to plain text mode 

    I am doing the above code, but now things are running very quickly and I cant see any animation, only the second view. The first parent view is visible but behind the second view.
    Last edited by das.joyita; 5th December 2016 at 11:32.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,056
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Load new view (.ui) from the previous view

    Qt Code:
    1. void MainWindow::dissolve( QWidget * pFadeOut, QWidget * pFadeIn )
    2. {
    3. QByteArray opacityProp( QString( "windowOpacity" ).toLatin1() );
    4. QPropertyAnimation * pFadeOutAnimation = new QPropertyAnimation( pFadeOut, opacityProp );
    5. QPropertyAnimation * pFadeInAnimation = new QPropertyAnimation( pFadeIn, opacityProp );
    6.  
    7. int duration = 3000;
    8. pFadeOutAnimation->setDuration( duration );
    9. pDafeOutAnimation->setStartValue( 1.0 );
    10. pFadeOutAnimation->setEndValue( 0.0 );
    11.  
    12. // Hide the fade out widget when the animation has stopped
    13. connect( pFadeOutAnimation, &QPropertyAnimation::finished, pFadeOut, &QWidget::hide );
    14.  
    15. pFadeInAnimation->setDuration( duration );
    16. pFadeInAnimation->setStartValue( 0.0 );
    17. pFadeInAnimation->setEndValue( 1.0 );
    18.  
    19. // Ensure the fade in widget is showing, but make it invisible by setting opacity to 0
    20. if ( !pFadeIn->isVisible() )
    21. {
    22. pFadeIn->setWindowOpacity( 0.0 );
    23. pFadeIn->show();
    24. }
    25.  
    26. pFadeOutAnimation->start( QAbstractAnimation::DeleteWhenStopped );
    27. pFadeInAnimation->start( QAbstractAnimation::DeleteWhenStopped );
    28. }
    To copy to clipboard, switch view to plain text mode 

    Not tested, so may require some edits.
    <=== 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.

  10. #10
    Join Date
    Nov 2016
    Posts
    9
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Load new view (.ui) from the previous view

    Thanks so much. Its works beautifully. You are a savior

Similar Threads

  1. Replies: 5
    Last Post: 2nd June 2016, 08:42
  2. Building Tree view from Table view which is already build.
    By DURGAPRASAD NEELAM in forum Newbie
    Replies: 6
    Last Post: 18th May 2015, 07:18
  3. view return stored procedure in table view
    By baradar in forum Qt Programming
    Replies: 5
    Last Post: 15th November 2014, 19:24
  4. How to switch view in sliding/moving new view from right of device to the left
    By curiouswalker in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th November 2010, 11:55
  5. Replies: 5
    Last Post: 21st November 2007, 20:38

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.