PDA

View Full Version : QGraphicsView FitInView only works if view is active view.



aguayro
10th July 2013, 02:11
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


QStringList flyerList;
QGraphicsScene FlyerScene;
QGraphicsPixmapItem *flyerItem;
QTimer flyerTimer;
int FlyerSwapPos;



Fragment of cpp file:


void RetroSuite::MEDIA_Flyer() {
FlyerScene.clear();
flyerList.clear();

QSettings inisets(QString("data/systemdata/%1/%2.ini").arg(CurrType).arg(CurrSID), QSettings::IniFormat);
QString folder = inisets.value("Flyer/Folder").toString();

if (!folder.isEmpty() && folder.length() > 3) {

QDirIterator iter(folder, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (iter.hasNext()) {
QFileInfo nextfile = iter.next();
if ((CurrGameName.contains(nextfile.completeBaseName( )) && CurrType != "Arcade") || (nextfile.completeBaseName() == ( CurrLaunchName) && CurrType == "Arcade")) {
flyerList << nextfile.absoluteFilePath();
}
}
if (flyerList.count() > 0) {

flyerItem = FlyerScene.addPixmap(QPixmap());
flyerItem->setPixmap(QPixmap(flyerList.at(0)));
ui->flyerView->setFlyerItem(flyerItem);

FlyerScene.setSceneRect(QPixmap(flyerList.at(0)).r ect());

FlyerSwapPos = 0;

if (inisets.value("Flyer/Fit").toBool()) {
ui->flyerView->setTransform(QTransform());
ui->flyerView->fitInView(flyerItem, Qt::KeepAspectRatio);
ui->flyerView->show();

} else if (inisets.value("Flyer/Scaled").toBool()) {
ui->flyerView->resetTransform();

switch( inisets.value("Flyer/SizeScale").toInt() ) {
case 0:
ui->flyerView->scale(0.1, 0.1);
break;
case 1:
ui->flyerView->scale(0.2, 0.2);
break;
case 2:
ui->flyerView->scale(0.3, 0.3);
break;
case 3:
ui->flyerView->scale(0.4, 0.4);
break;
case 4:
ui->flyerView->scale(0.5, 0.5);
break;
case 5:
ui->flyerView->scale(0.6, 0.6);
break;
case 6:
ui->flyerView->scale(0.7, 0.7);
break;
case 7:
ui->flyerView->scale(0.8, 0.8);
break;
case 8:
ui->flyerView->scale(0.9, 0.9);
break;
case 9:
ui->flyerView->scale(1.0, 1.0);
break;

}
} else {
ui->flyerView->setTransform(QTransform());
}
}
if (inisets.value("Flyer/SwapAuto").toBool()) {
flyerTimer->start( inisets.value("Flyer/SwapTimer").toInt() * 1000 );
}
}


}



I'm really lost with this, i have no idea what is wrong.

ChrisW67
10th July 2013, 05:25
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.

aguayro
10th July 2013, 12:27
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.



void RetroSuite::MEDIA_Cartridge() {
CartScene.clear();
cartList.clear();

QSettings inisets(QString("data/systemdata/%1/%2.ini").arg(CurrType).arg(CurrSID), QSettings::IniFormat);

QString folder = inisets.value("Cartridge/Folder").toString();
if (!folder.isEmpty() && folder.length() > 3) {

QDirIterator iter(folder, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (iter.hasNext()) {
QFileInfo nextfile = iter.next();
if ((CurrGameName.contains(nextfile.completeBaseName( )) && CurrType != "Arcade") || (nextfile.completeBaseName() == ( CurrLaunchName) && CurrType == "Arcade")) {
cartList << nextfile.absoluteFilePath();
}
}

if (cartList.count() > 0) {

cartItem = CartScene.addPixmap(QPixmap());
cartItem->setPixmap(QPixmap(cartList.at(0)));
ui->cartView->setCartItem(cartItem);

CartScene.setSceneRect(QPixmap(cartList.at(0)).rec t());

CartSwapPos = 0;

if (inisets.value("Cartridge/Fit").toBool()) {
ui->cartView->setTransform(QTransform());
ui->cartView->fitInView(cartItem, Qt::KeepAspectRatio);
ui->cartView->show();

} else if (inisets.value("Cartridge/Scaled").toBool()) {
ui->cartView->resetTransform();

switch( inisets.value("Cartridge/SizeScale").toInt() ) {
case 0:
ui->cartView->scale(0.1, 0.1);
break;
case 1:
ui->cartView->scale(0.2, 0.2);
break;
case 2:
ui->cartView->scale(0.3, 0.3);
break;
case 3:
ui->cartView->scale(0.4, 0.4);
break;
case 4:
ui->cartView->scale(0.5, 0.5);
break;
case 5:
ui->cartView->scale(0.6, 0.6);
break;
case 6:
ui->cartView->scale(0.7, 0.7);
break;
case 7:
ui->cartView->scale(0.8, 0.8);
break;
case 8:
ui->cartView->scale(0.9, 0.9);
break;
case 9:
ui->cartView->scale(1.0, 1.0);
break;


}
} else {
ui->cartView->setTransform(QTransform());
}
}
if (inisets.value("Cartridge/SwapAuto").toBool()) {
cartTimer->start( inisets.value("Cartridge/SwapTimer").toInt() * 1000 );
}
}

}





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



void rs_Graphicsview::resizeEvent(QResizeEvent *event) {
fitInView(this->scene()->sceneRect(), Qt::KeepAspectRatio);
}