Unsolved Is it the proper way to change QWidgets in QGridLayout
-
Hi,
I will start with a picture:
This is the same myWidget ( it inherits QWidget ) and I have QGridLayout on it and in this QGridLayout I have other QGridLayouts and QWidgets.
The situation one is when I have mouse cursor outside this myWidget ( hover leave ) and the situation two is when I have mouse cursor inside this myWidget ( hover enter ).
Widgets in situation one and two are the same ( on situation two I have additional Widgets ). So when I would like to move my mouse cursor inside myWidget I have to show some widgets and when I would like to move mouse cursor outside myWidget I have to hide some widgets.
My code looks like this:
myWidget::myWidget(QWidget *parent): QWidget(parent) { setAttribute(Qt::WA_Hover); gridLayout = new QGridLayout; gridLabels = new QGridLayout; gridUpper = new QGridLayout; setLayout(gridLayout); avatar= new QLabel; name= new QLabel; date= new QLabel; menu= new QLabel; lastMessage= new QLabel; avatar->setStyleSheet("QLabel {border: 1px solid black}"); name->setStyleSheet("QLabel {border: 1px solid black}"); date->setStyleSheet("QLabel {border: 1px solid black}"); menu->setStyleSheet("QLabel {border: 1px solid black}"); lastMessage->setStyleSheet("QLabel {border: 1px solid black}"); gridLayout->addWidget(avatar,0,0); gridLayout->addLayout(gridLabels,0,1); gridLabels->addLayout(gridUpper,0,0); gridLabels->addWidget(lastMessage,1,0); gridLabels->addWidget(menu,0,1,2,1); gridUpper->addWidget(name, 0,0); gridUpper->addWidget(date, 0,1); gridLayout->setColumnStretch(0,2); gridLayout->setColumnStretch(1,5); gridUpper->setColumnStretch(0,5); gridUpper->setColumnStretch(1,0); gridLabels->setColumnStretch(0,4); gridLabels->setColumnStretch(1,0); date->hide(); menu->hide(); resize(500,150); } bool myWidget::event(QEvent *event) { if(event->type()==QEvent::HoverEnter) { date->show(); menu->show(); gridUpper->setColumnStretch(0,5); gridUpper->setColumnStretch(1,2); gridLabels->setColumnStretch(0,4); gridLabels->setColumnStretch(1,1); } else if(event->type()==QEvent::HoverLeave) { date->hide(); menu->hide(); gridUpper->setColumnStretch(0,5); gridUpper->setColumnStretch(1,0); gridLabels->setColumnStretch(0,4); gridLabels->setColumnStretch(1,0); } return QWidget::event(event); }
"menu" and "date" are this additional widgets, which are in in situation two, and aren't in situation one.
And question: Is my way to change widgets in QGridLayout is proper? I hide / show this additional widgets and change columnStretch ( when I would like to hide widgets I set that column to 0 ). Is there a better idea?
-
@TomNow99
I have no idea whether it would be "better" or "worse" then your way ofQGridLayout
and stretching/shtinking column widths. But this could (presumably) all be implemented viaQHBoxLayout
s &QVBoxLayout
s.The whole thing is as per #2: is hbox, with 3 vboxes; middle one is 2 vboxes, with top cell being 2 hboxes.
Then #1 is hide/show some widgets/boxes: the right-hand vbox is empty, and so is the right-hand one in the vbox in the middle.
My explanation might not be great, but you should get the drift. That allows the boxes to flow to fill the layouts. Whether it's beneficial I don't know. I just come from HTML where in this kind of alyout
div
s (Q(H|V)BoxLayout
) are good andtable
s (QGridLayout
) are devil-spawn ;-)