0
votes
qt - Child widget mouse event - Stack Overflow
Asked
Viewed 2k times
0

I have a simple Qt application that has a mainwindow and a class (TreeView) that inherits QTreeWidget.

I can't seem to enable the mouse events for the treewidget. The mainwindow with toolbar and menubar works fine but I'm not able to click any of my items in the treeWidget.

I have included QMouseEvent in my class and implemented the virtual functions. Here is one for an example:

void TreeView::itemDoubleClicked(QTreeWidgetItem *item, int column)
{
    QMessageBox::information(this, "Message", item->text(column));
}

Am I missing something obvious here?

EDIT: My TreeView.h:

#include <vector>
#include <algorithm>
#include <QTreeWidget>
#include <QMouseEvent>

//ObjectCube includes
#include <Hub.h>
#include <TagSet/TagSet.h>
#include <Tag/Tag.h>

#include "tagsettree.h"
#include "tagtree.h"

using namespace ObjectCube;

class TreeView: public QTreeWidget
{
    Q_OBJECT
public:
    TreeView(QWidget *parent);    
    void buildTree(Hub *hub);
    void buildEachTagSetTree(TagSetTree *parent);
    void addHierarchiesToTree(QTreeWidgetItem *parent, TagSet *tagSet);

    void itemDoubleClicked (QTreeWidgetItem *item, int column);
};

The Constructor:

TreeView::TreeView(QWidget *parent): QTreeWidget(parent)
{
    this->setHeaderLabel("Images");
    this->setEnabled(true);
}

EDIT:

I have fixed what @webclectic pointed out but it has not fixed the problem.

Is there a possibility that I have blocked all mouseEvents in the treeWidget? I can iterate through them with the keyboard but the mouse does not work.

EDIT:

I have a main window. That main window has a pointer to a controller class. The controller class owns instances of all the widgets used in the mainwindow, including the TreeWidget. The main window is the parent of the controller as well as all of the widgets inside of it. Code:

//Constructor
MainController::MainController(QWidget *parent, Hub *hub): QWidget(parent)
{
    this->hub = hub;
    initializeController(parent);
}

void MainController::initializeController(QWidget *main)
{
    treeView = new TreeView(main);
    glWidget = new GLWidget(main);
    listWidget = new ListWidget(main);

    rightSide = new QVBoxLayout(main);

    treeView->buildTree(hub);

    treeView->move(0, 55);
    treeView->resize(main->frameGeometry().width() * 0.2, main->frameGeometry().height() *0.91);

    glWidget->move(main->frameGeometry().width() * 0.32 ,55);
    glWidget->resize(main->frameGeometry().width() * 0.67, main->frameGeometry().height() *0.91);

    rightSide->addWidget(listWidget);
    rightSide->setGeometry(QRect(QPoint(main->geometry().width() -20 , 55), QPoint(main->geometry().width(),main->geometry().height())));

}

Gísli

8
  • can u provide some more detail....
    – shofee
    Mar 7 2012 at 13:12
  • @shobi added my .h file and the constructor
    – Gisli
    Mar 7 2012 at 13:16
  • 1
    have u seen the answer of webclectic , try that...
    – shofee
    Mar 7 2012 at 13:19
  • 2
    What happens if you add a QTreeWidget instead of your class? You can also reimplement the mousePressEvent() or mouseDoubleClickEvent() and add a qDebug message inside them to see what is going on. Try to create a minimal example where the problem is still present, and you will find what is going wrong...
    – pnezis
    Mar 7 2012 at 13:35
  • Also, might be interesting to tell how you add your widget to the Main Window. Mar 7 2012 at 13:58
1

itemDoubleClicked is a signal and not a slot. What you need to do is to connect it with a slot, where you will have your custom code.

In the mainwindow's constructor

// pTreeView is a pointer to the TreeView widget
connect(pTreeView, SIGNAL(itemDoubleClicked ( QTreeWidgetItem *, int),
        this,      SLOT(mySlot(QTreeWidgetItem*, int)));

And then in your slot:

void TreeView::mySlot(QTreeWidgetItem *item, int column)
{
    QMessageBox::information(this, "Message", item->text(column));
}
1
  • Thanks for that. This did not do the trick though. It is still impossible to click any item in the widget.
    – Gisli
    Mar 7 2012 at 13:24
0

So I finally found the answer and it was a silly mistake. In the main window constructor I created instances of the widgets before I did:

ui->setupUi(this);

So I moved this line to the top of the constructor and everything worked. This is probably a basic thing in Qt but I missed it for some reason.

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.

     
    2
    can u provide some more detail....shofee
    @shobi added my .h file and the constructorGisli
    have u seen the answer of webclectic , try that...shofee
    What happens if you add a QTreeWidget instead of your class? You can also reimplement the mousePressEvent() or mouseDoubleClickEvent() and add a qDebug message inside them to see what is going on. Try to create a minimal example where the problem is still present, and you will find what is going wrong...pnezis
    Also, might be interesting to tell how you add your widget to the Main Window.Chris Browet

    2 Answers

    1
    votes

    itemDoubleClicked is a signal and not a slot. What you need to do is to connect it with a slot, where you will have your custom code.

    In the mainwindow's constructor

    // pTreeView is a pointer to the TreeView widget
    connect(pTreeView, SIGNAL(itemDoubleClicked ( QTreeWidgetItem *, int),
            this,      SLOT(mySlot(QTreeWidgetItem*, int)));
    

    And then in your slot:

    void TreeView::mySlot(QTreeWidgetItem *item, int column)
    {
        QMessageBox::information(this, "Message", item->text(column));
    }
    
    0
    votes

    So I finally found the answer and it was a silly mistake. In the main window constructor I created instances of the widgets before I did:

    ui->setupUi(this);
    

    So I moved this line to the top of the constructor and everything worked. This is probably a basic thing in Qt but I missed it for some reason.