Results 1 to 3 of 3

Thread: QGraphicsView FitInView only works if view is active view.

  1. #1
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QGraphicsView FitInView only works if view is active view.

    I have an app with a QTabWidget containing 5 tabs. Each tab has 1 QGraphicsView.
    The first time i run the application, the fitInView function only works on active QGraphicsView, after I've change to all tabs, the fitInView function keeps working correctly.
    It's like i have to use it at least 1 time with each tab active for got it working correctly.
    Any idea what could be wrong? Something about fitInView or QGraphicsView that i should to know?
    It's a lot of code, i don't know what to copy/paste >_<, this is the code used each time an image is loaded (this is the code of 1 of the 5 QGraphicsView, all are almost the same):


    Header File
    Qt Code:
    1. QStringList flyerList;
    2. QGraphicsScene FlyerScene;
    3. QTimer flyerTimer;
    4. int FlyerSwapPos;
    To copy to clipboard, switch view to plain text mode 

    Fragment of cpp file:
    Qt Code:
    1. void RetroSuite::MEDIA_Flyer() {
    2. FlyerScene.clear();
    3. flyerList.clear();
    4.  
    5. QSettings inisets(QString("data/systemdata/%1/%2.ini").arg(CurrType).arg(CurrSID), QSettings::IniFormat);
    6. QString folder = inisets.value("Flyer/Folder").toString();
    7.  
    8. if (!folder.isEmpty() && folder.length() > 3) {
    9.  
    10. QDirIterator iter(folder, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
    11. while (iter.hasNext()) {
    12. QFileInfo nextfile = iter.next();
    13. if ((CurrGameName.contains(nextfile.completeBaseName()) && CurrType != "Arcade") || (nextfile.completeBaseName() == ( CurrLaunchName) && CurrType == "Arcade")) {
    14. flyerList << nextfile.absoluteFilePath();
    15. }
    16. }
    17. if (flyerList.count() > 0) {
    18.  
    19. flyerItem = FlyerScene.addPixmap(QPixmap());
    20. flyerItem->setPixmap(QPixmap(flyerList.at(0)));
    21. ui->flyerView->setFlyerItem(flyerItem);
    22.  
    23. FlyerScene.setSceneRect(QPixmap(flyerList.at(0)).rect());
    24.  
    25. FlyerSwapPos = 0;
    26.  
    27. if (inisets.value("Flyer/Fit").toBool()) {
    28. ui->flyerView->setTransform(QTransform());
    29. ui->flyerView->fitInView(flyerItem, Qt::KeepAspectRatio);
    30. ui->flyerView->show();
    31.  
    32. } else if (inisets.value("Flyer/Scaled").toBool()) {
    33. ui->flyerView->resetTransform();
    34.  
    35. switch( inisets.value("Flyer/SizeScale").toInt() ) {
    36. case 0:
    37. ui->flyerView->scale(0.1, 0.1);
    38. break;
    39. case 1:
    40. ui->flyerView->scale(0.2, 0.2);
    41. break;
    42. case 2:
    43. ui->flyerView->scale(0.3, 0.3);
    44. break;
    45. case 3:
    46. ui->flyerView->scale(0.4, 0.4);
    47. break;
    48. case 4:
    49. ui->flyerView->scale(0.5, 0.5);
    50. break;
    51. case 5:
    52. ui->flyerView->scale(0.6, 0.6);
    53. break;
    54. case 6:
    55. ui->flyerView->scale(0.7, 0.7);
    56. break;
    57. case 7:
    58. ui->flyerView->scale(0.8, 0.8);
    59. break;
    60. case 8:
    61. ui->flyerView->scale(0.9, 0.9);
    62. break;
    63. case 9:
    64. ui->flyerView->scale(1.0, 1.0);
    65. break;
    66.  
    67. }
    68. } else {
    69. ui->flyerView->setTransform(QTransform());
    70. }
    71. }
    72. if (inisets.value("Flyer/SwapAuto").toBool()) {
    73. flyerTimer->start( inisets.value("Flyer/SwapTimer").toInt() * 1000 );
    74. }
    75. }
    76.  
    77.  
    78. }
    To copy to clipboard, switch view to plain text mode 

    I'm really lost with this, i have no idea what is wrong.
    Always trying to learn >.<

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGraphicsView FitInView only works if view is active view.

    It's not clear but if line 30 is the first time that the view is show()n then at line 29 the view has no dimensions and, as the QGraphicsView::fitInView() docs say:
    If rect is empty, or if the viewport is too small, this function will do nothing.
    After the first time through this code the views are all visible and have dimensions.

  3. #3
    Join Date
    Jan 2012
    Location
    Canary Islands, Spain
    Posts
    86
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QGraphicsView FitInView only works if view is active view.

    The code works perfect when i click the first image but only in the active tab, if i change to another tab, the iamge is small and centered on the view.
    The "fix" is only applied if the view is currently on the screen, inf not, it keeps showing a small and centered image, until i click that tab, then it's stay fixed.
    If i set sceneRect when i start the app, nothing changes.

    I can't see the difference, but this code of another tab/view works perfect, it loads fitted as i want. This is strange.... basically is the saem code with some variables/objects names changed.

    Qt Code:
    1. void RetroSuite::MEDIA_Cartridge() {
    2. CartScene.clear();
    3. cartList.clear();
    4.  
    5. QSettings inisets(QString("data/systemdata/%1/%2.ini").arg(CurrType).arg(CurrSID), QSettings::IniFormat);
    6.  
    7. QString folder = inisets.value("Cartridge/Folder").toString();
    8. if (!folder.isEmpty() && folder.length() > 3) {
    9.  
    10. QDirIterator iter(folder, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
    11. while (iter.hasNext()) {
    12. QFileInfo nextfile = iter.next();
    13. if ((CurrGameName.contains(nextfile.completeBaseName()) && CurrType != "Arcade") || (nextfile.completeBaseName() == ( CurrLaunchName) && CurrType == "Arcade")) {
    14. cartList << nextfile.absoluteFilePath();
    15. }
    16. }
    17.  
    18. if (cartList.count() > 0) {
    19.  
    20. cartItem = CartScene.addPixmap(QPixmap());
    21. cartItem->setPixmap(QPixmap(cartList.at(0)));
    22. ui->cartView->setCartItem(cartItem);
    23.  
    24. CartScene.setSceneRect(QPixmap(cartList.at(0)).rect());
    25.  
    26. CartSwapPos = 0;
    27.  
    28. if (inisets.value("Cartridge/Fit").toBool()) {
    29. ui->cartView->setTransform(QTransform());
    30. ui->cartView->fitInView(cartItem, Qt::KeepAspectRatio);
    31. ui->cartView->show();
    32.  
    33. } else if (inisets.value("Cartridge/Scaled").toBool()) {
    34. ui->cartView->resetTransform();
    35.  
    36. switch( inisets.value("Cartridge/SizeScale").toInt() ) {
    37. case 0:
    38. ui->cartView->scale(0.1, 0.1);
    39. break;
    40. case 1:
    41. ui->cartView->scale(0.2, 0.2);
    42. break;
    43. case 2:
    44. ui->cartView->scale(0.3, 0.3);
    45. break;
    46. case 3:
    47. ui->cartView->scale(0.4, 0.4);
    48. break;
    49. case 4:
    50. ui->cartView->scale(0.5, 0.5);
    51. break;
    52. case 5:
    53. ui->cartView->scale(0.6, 0.6);
    54. break;
    55. case 6:
    56. ui->cartView->scale(0.7, 0.7);
    57. break;
    58. case 7:
    59. ui->cartView->scale(0.8, 0.8);
    60. break;
    61. case 8:
    62. ui->cartView->scale(0.9, 0.9);
    63. break;
    64. case 9:
    65. ui->cartView->scale(1.0, 1.0);
    66. break;
    67.  
    68.  
    69. }
    70. } else {
    71. ui->cartView->setTransform(QTransform());
    72. }
    73. }
    74. if (inisets.value("Cartridge/SwapAuto").toBool()) {
    75. cartTimer->start( inisets.value("Cartridge/SwapTimer").toInt() * 1000 );
    76. }
    77. }
    78.  
    79. }
    To copy to clipboard, switch view to plain text mode 


    After some tests, i've fixed it with this:

    Qt Code:
    1. void rs_Graphicsview::resizeEvent(QResizeEvent *event) {
    2. fitInView(this->scene()->sceneRect(), Qt::KeepAspectRatio);
    3. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by aguayro; 10th July 2013 at 12:17.
    Always trying to learn >.<

Similar Threads

  1. pan view of QGraphicsView
    By wagmare in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2013, 10:33
  2. Replies: 1
    Last Post: 11th July 2011, 20:39
  3. QGraphicsView: Save as PNG without displaying View?
    By SixDegrees in forum Qt Programming
    Replies: 0
    Last Post: 8th October 2010, 22:56
  4. Using QGraphicsView with model/view programming
    By JLP in forum Qt Programming
    Replies: 3
    Last Post: 29th January 2007, 11:04
  5. graphics view FitInView problem
    By aamer4yu in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2007, 10:24

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.