Solved Double click on child widget
-
Greetings,
I am new to QT so I don't know much about it, but I have encountered a problem with a widget that is a child inside UI parent.
I have class, named GraphWidget which shows graph with nodes and vertices. This class is inherited from QGraphicsView. Inside this class, in it's constructor I create this graph, using 2 other classes Node and Vertices (They both are inherited from QGraphicsItem),and QGraphicsScene (to create widget and draw graph). In Node class I have enabled MouseDoubleClickEvent. When I double click on some node, it opens a new window.
This works great, except when my GraphWidget object is a child of MainWindow, like this:MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow) { GraphWidget *w = new GraphWidget(this); //this line w->show(); ui->setupUi(this); }
When I create object w like this, then I can't double click on my node, because parent GUI takes control over it (I guess). I wanted to use SIGNALS and SLOTS but I don't know how can I emit double click from MainWindow when it's inherited from QMainWindow and it doesn't have double click.
Please if you can help I would be very thankful.
-
@Annabela_Cortez You should create and show GraphWidget AFTER you initialised your main window:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow) { ui->setupUi(this); GraphWidget *w = new GraphWidget(this); //this line w->show(); }
How does your event handler for mouse double click look like?
-
This post is deleted! -
@jsulm Oh wow, this works. Thank you so much :)
But please, could you explain me what is the difference if I create main window first, why does it work? -
@Annabela_Cortez
Hi
If you do like this ( the wrong way)
GraphWidget *w = new GraphWidget(this);
ui->setupUi(this);
Then all kinds of widgets (if any in the UI) will be put on top of your GraphWidget.
MainWindow has a special center widget called centralWidget() that will/might cover your
widget when the order of insertion is wrong.
Even if a widget is transparent, it will/might still eat mouse events.
So rule is
ui->setupUi(this); always comes first.