Problem with tableWidget component when QGraphicsOpacityEffect gradient reverts to opacity
-
When I use QGraphicsOpacityEffect to implement a gradient toggle for stackedWidgets, the tablewidget component on page 0 of the stackedWidget is not being restored to opacity properly, as indicated by the fact that its center background is restored to opacity but the column headers and sliders are transparent.
The code is as follows:
void MainWindow::on_pushButton_clicked() { QGraphicsOpacityEffect *effectOut = new QGraphicsOpacityEffect(); QWidget *currentWidget = ui->stackedWidget->currentWidget(); currentWidget->setGraphicsEffect(effectOut); QPropertyAnimation *animationOut = new QPropertyAnimation(effectOut, "opacity"); animationOut->setDuration(100); animationOut->setStartValue(1.0); animationOut->setEndValue(0.0); QGraphicsOpacityEffect *effectIn = new QGraphicsOpacityEffect(); QWidget *nextWidget = ui->stackedWidget->widget(0); nextWidget->setGraphicsEffect(effectIn); QPropertyAnimation *animationIn = new QPropertyAnimation(effectIn, "opacity"); animationIn->setDuration(100); animationIn->setStartValue(0.0); animationIn->setEndValue(1.0); connect(animationOut, &QPropertyAnimation::finished, this, [this, nextWidget]() { ui->stackedWidget->setCurrentIndex(ui->stackedWidget->indexOf(nextWidget)); ui->tableWidget->setGraphicsEffect(nullptr); ui->tableWidget->repaint(); QCoreApplication::processEvents(); }); animationOut->start(QAbstractAnimation::DeleteWhenStopped); animationIn->start(QAbstractAnimation::DeleteWhenStopped); }
I even tried to rewrite the tablewidget, but it still didn't work. The strange thing is that when I click on another program or click a bit on the tablewidget, it immediately reverts to opacity.
I tried using update and repain, but that doesn't work either. I hope you can help me with this issue, thanks a lot!
-
@2hours said in Problem with tableWidget component when QGraphicsOpacityEffect gradient reverts to opacity:
When I use QGraphicsOpacityEffect to implement a gradient toggle for stackedWidgets, the tablewidget component on page 0 of the stackedWidget is not being restored to opacity properly, as indicated by the fact that its center background is restored to opacity but the column headers and sliders are transparent.
I'm not sure, can you show or explain further what you expect?
I don't know what
it immediately reverts to opacity
means, but some things I've noticed:
This line is meaningless
ui->tableWidget->setGraphicsEffect(nullptr);
since these lines
QWidget *currentWidget = ui->stackedWidget->currentWidget(); // ... QWidget *nextWidget = ui->stackedWidget->widget(0);
return a plain "page"
QWidget
which holds yourQTableWidget
. So theQGraphicsOpacityEffect
is applied to thepage
Widget, not theQTableView
itself.
(when you did everything in Designer)Also there might be something wrong with your logic... if you want to do what I think, then starting both animations at the same time and with same duration doesn't make any sense.
Start the Fade-out, when finished, start the fade-in and flip the page of yourQStackedWidget
.It works for me, but some parts of the
QTableWidget
seem to ignore the opacity effect at all... they are never opaque or transparent. The background of the header and other widgets on the same page fade-in though.Edit:
When setting the effect to theQTableWidget
's viewport, the content fades in correctly (the header still doesn't)Seems like
QTableWidget
doesn't like being transparent :D
Even though, setting the effect to theQStackedWidget
' s page widget, should apply it to all children (as stated here)