PushButtons on QGraphicsView aren't clickable - qt

I want to overlay a QGLWidget with clickable buttons.
I'm using a QGraphicsView and setting the viewport to a QGLWidget. Then I'm adding a layout to the QGraphicsView, and adding several QPushButtons to that.
The Buttons display correctly, HOWEVER they are not clickable.
Here is my code:
QGraphicsView* view = ui->gView;
QGLWidget* gl = new QGLWidget(QGLFormat(QGL::SampleBuffers));
view->setViewport(gl);
QPushButton* push = new QPushButton("Click me");
QHBoxLayout* h = new QHBoxLayout;
QPushButton* wo = new QPushButton("Wo");
h->addWidget(wo);
h->addWidget(push);
view->setLayout(h);
Is there a simple way I can make the buttons get the clicks when the mouse is above them, and QGLWidget get the clicks otherwise?

Related

Can we shift widget between the cells of a table widget

I've created a a widget in which I've placed two buttons using a layout and placed it inside a table widget's cell. The thing is that I'm changing the size of the table and I want to shift the placement of the widget without deleting them and re-initializing them from the beginning because I already assigned them actions on click (I think that the application would crash in this situation)
Code:
btn = new QPushButton[horzHeaders.size()];
btn[j].setParent(ui->tableWidget);
btn[j].setIcon(QIcon("./save.png"));
btn[j].setVisible(true);
btn_Load = new QPushButton[horzHeaders.size()];
btn_Load[j].setParent(ui->tableWidget);
btn_Load[j].setIcon(QIcon("./upload.png"));
btn_Load[j].setVisible(true);
lay = new QHBoxLayout[horzHeaders.size()];
lay[j].addWidget(&btn[j]);
lay[j].addWidget(&btn_Load[j]);
QWidget *w = new QWidget[horzHeaders.size()];
w[j].setLayout(&lay[j]);
ui->tableWidget->setCellWidget(j,vertHeaders.size() - 1, &w[j]);
You can add and remove widgets from a layout anytime you want
QWidget *widget = new QWidget();
QPushButton *button = new QPushButton();
QHBoxLayout *Hbox = new QHBoxLayout();
QVBoxLayout *Vbox= new QVBoxLayout();
Hbox->addWidget(button);
// use it till the window is resized
//and then check with an if-statement if the window is resized or not
Hbox->removeWidget(button); // remove from the button from layout
layout()->removeAt(widget); //remove the widget's current layout
Vbox->addWidget(button); // add button widget to vertical layout
widget->setLayout(Vbox); // Give it a new layout
widget->setLayout(Vbox);
If this didn't answer your question then ask in the comments.

Small panel at the bottom of a QTreeWidget

I'm using QT 5.4.2 and trying to create a small panel at the bottom
of a subclassed QTreeWidget.
Here is the code:
void HmiScenarioAutoscriptPanel::searchEmitter() {
QWidget *child = new QWidget(ui->emitterTreeWidget);
//QMainWindow* child = new QMainWindow;
QLabel *labelSearch = new QLabel("Search");
QLineEdit *lineSearch = new QLineEdit();
lineSearch->setFixedSize(100, 20);
QHBoxLayout* layout = new QHBoxLayout(ui->emitterTreeWidget);
layout->setAlignment(Qt::AlignBottom);
layout->addWidget(child);
layout->addWidget(labelSearch);
layout->addWidget(lineSearch);
}
The label and search field correctly appear at the bottom of the tree,
however the fields overlap with the tree nodes (see image below).
Any idea why this behavior?
Ciao
Alf
enter image description here
It is not recommended to set layout on the tree widget. It is like other controls like a button, label etc..
I see that you are using designer. Add a blank widget (searchWidget) under the tree widget and then
void HmiScenarioAutoscriptPanel::searchEmitter() {
QWidget *child = new QWidget(ui->searchWidget);
//QMainWindow* child = new QMainWindow;
QLabel *labelSearch = new QLabel("Search", searchWidget);
QLineEdit *lineSearch = new QLineEdit(searchWidget);
lineSearch->setFixedSize(100, 20);
QHBoxLayout* layout = new QHBoxLayout(ui->searchWidget);
layout->setAlignment(Qt::AlignBottom);
layout->addWidget(child);
layout->addWidget(labelSearch);
layout->addWidget(lineSearch);
}
Just out of curiosity, why don't you add these using the designer as well?

QWidgetAction spills over menu icon column

Using Qt 4.8.2, on Windows.
I'm putting user information in a QWidgetAction for a drop-down menu. But the QWidgetAction's widget is spilling over the left column where action icons are normally displayed:
Here's the code:
QMenu *menu = new QMenu;
QIcon icon(...);
QLabel *iconLabel = new QLabel;
iconLabel->setPixmap(icon.pixmap(32, 32)); // XXX what size?
QLabel *userName = new QLabel("user name");
QLabel *userEmail = new QLabel("Rfoo#bar.com");
// Layouts
QVBoxLayout* vbox = new QVBoxLayout();
vbox->addWidget(userName);
vbox->addWidget(userEmail);
QHBoxLayout* hbox = new QHBoxLayout;
hbox->addWidget(iconLabel);
hbox->addLayout(vbox);
// container widget
QWidget* widget = new QWidget;
widget->setLayout(hbox);
// Widget Action
QWidgetAction* widgetAction = new QWidgetAction(menu);
widgetAction->setDefaultWidget(widget);
menu->addAction(widgetAction);
menu->addSeparator();
menu->addAction("P....... P.......");
menu->addSeparator();
menu->addAction("Sign Out");
If I remove the pixmap from the widget and set it as the QWidgetAction's icon, the widget still spills over the left column and the icon is not displayed.
I'd like to figure out one of these solutions:
keep the QWidgetAction from spilling into the menu's icon column
set the QWidgetAction's icon and have it displayed in the icon column
remove the menu's icon column completely
Thanks

Adding QGraphicsView and QGraphicsScene to a label in Qt

I have a stacked widget on which I have a QLabel. I display an image on this label. I want to zoom and pan this image and I am trying to use the QGraphicsView technique. However a new window opens.
This is what I am doing.
scene = new QGraphicsScene(this);
view = new QGraphicsView(this);
QPixmap pix("/root/Image);
label->setPixmap(pix);
scene->addWidget(label);
view->setScene(scene);
view->setDragMode(QGraphicsView::scrollHandDrag);
view->show();
Can someone please suggest me what I should do. I want the label to act like the QGraphicsView.
Thank You :)
You create a scene and view and add the label to the scene, then tell the view to show itself with: -
view->show()
If, as you state, you want the QGraphicsView on the label, don't add the label to the scene, but add the QGraphicsView to the label: -
QLabel* pLabel = new QLabel(mainWindow); // setting mainWindow as the parent
QGraphicsView* pView = new QGraphicsView(pLabel) // set the label as the parent of the view.
You do not need to call show, as the label will handle that for you, assuming the label is on a Widget that is already displayed.
Now, instead of setting the pixmap on the label, create a pixmap in the scene with: -
pScene->addPixmap(pixmap);

How to implement Menu bar inside QGraphicsView?

I have a QGraphicsView which shows images dynamically. I also used fitInView feature to resize the window. Now I need to add the menu bar at the top of the QGraphicsView. How to implement this? Please help. I am new in Qt
As beginner it is probably easiest to place your QGraphicsView into a QVBoxLayout and create a QMenuBar and insert it above your graphics view.
QMenuBar *bar = new QMenuBar();
ui->yourVerticalLayout->insertWidget( 0, bar );
QMenu* yourMenu = bar->addMenu("Your Menu title");
QAction* yourFirstAction = yourMenu->addAction("Your First Action");

Resources