Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Double click on child widget

Double click on child widget

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Annabela_Cortez
    wrote on last edited by Annabela_Cortez
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • A Annabela_Cortez

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      2
      • A Offline
        A Offline
        Annabela_Cortez
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • jsulmJ jsulm

          @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?

          A Offline
          A Offline
          Annabela_Cortez
          wrote on last edited by
          #4

          @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?

          mrjjM 1 Reply Last reply
          0
          • A Annabela_Cortez

            @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?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved