简体   繁体   English

QTreeWidget childAt(int x,int y)返回NULL

[英]QTreeWidget childAt(int x, int y) returns NULL

I am trying to get the QTreeWidgetItem (node) at the position where the mouse pointer is. 我正在尝试将QTreeWidgetItem(节点)放置在鼠标指针所在的位置。 The QTreeWidget class has a method called childAt(int x, int y) which does not seem to be documented here: http://qt-project.org/doc/qt-5.1/qtwidgets/qtreewidget.html and I have no idea why. QTreeWidget类有一个名为childAt(int x,int y)的方法 ,在这里似乎没有记录: http : //qt-project.org/doc/qt-5.1/qtwidgets/qtreewidget.html ,我不知道为什么。 May be there is a reason behind it. 可能背后有一个原因。 The method always returns NULL for me. 该方法总是为我返回NULL。

I have extended the QTreeWidget class so I can capture the mouseMoveEvent. 我扩展了QTreeWidget类,以便可以捕获mouseMoveEvent。

class CustomTreeWidget : public QTreeWidget 
{
  Q_OBJECT

  public:
      explicit CustomTreeWidget(QWidget *parent = 0);

  signals:
      void OnMouseMove(int x, int y);

  public slots:

  private:
      void mouseMoveEvent(QMouseEvent *event);

 };

Then in my main cpp file: 然后在我的主要cpp文件中:

void CustomTreeWidget::mouseMoveEvent(QMouseEvent *event)
{
    QTreeWidget::mouseMoveEvent(event);

    POINT p;
    if (GetCursorPos(&p))
    {
       qDebug(QString("GetCursorPos() OK: X=" + QString::number(p.x) + " Y=" + QString::number(p.y)).toLocal8Bit().data());

       QTreeWidgetItem *item = dynamic_cast<QTreeWidgetItem *> (this->childAt(p.x, p.y));

       if (item == NULL) return;

       qDebug(item->text(0).toLocal8Bit().data());
    }

    emit OnMouseMove(p.x, p.y);
}

Then in my MainWindow file: 然后在我的MainWindow文件中:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

{
   ui->setupUi(this);

   XTreeWidget *treeWidget = new XTreeWidget(this);

   QTreeWidgetItem *rootItem = new QTreeWidgetItem(treeWidget, QTreeWidgetItem::UserType);
   rootItem->setText(0, "Item 1");
   treeWidget->addTopLevelItem(rootItem);

   rootItem = new QTreeWidgetItem(treeWidget, QTreeWidgetItem::UserType);
   rootItem->setText(0, "Item 2");
   treeWidget->addTopLevelItem(rootItem);

   this->setCentralWidget(treeWidget);
}

I run the code and the following statement never runs: 我运行代码,并且以下语句永远不会运行:

qDebug(item->text(0).toLocal8Bit().data());

So in the mouseMoveEvent in the CustomTreeWidget class, the following statement returns true and the method returns: 因此,在CustomTreeWidget类的mouseMoveEvent中,以下语句返回true,并且该方法返回:

if (item == NULL) return;

What I am I doing wrong? 我做错了什么? dynamic_cast fails. dynamic_cast失败。

I also tried ScreenToClient((HWND)this->winId(), &p)) and passing px and py to childAt() as well as event.pos.x() and event.pos.y(). 我还尝试了ScreenToClient((HWND)this-> winId(),&p))并将px和py传递给childAt()以及event.pos.x()和event.pos.y()。 I am really confused. 我真的很困惑。

I checked my px and py in the log and they always valid. 我在日志中检查了px和py,它们始终有效。

I even show a tooltip at the x and y and they are valid, yet, childAt() fails. 我什至在x和y处显示工具提示,它们有效,但是childAt()失败。

The problem is that you use "the position of the mouse cursor, in screen coordinates.", but it requires coordinates in QTreeWidget's coordinates system. 问题是您使用“鼠标光标的位置,以屏幕坐标为单位。”,但是它需要QTreeWidget的坐标系中的坐标。 Thus I would suggest to drop using WinAPI and use QMouseEvent::pos() function instead. 因此,我建议删除使用WinAPI并改用QMouseEvent :: pos()函数。 Your mouseMoveEvent will look like: 您的mouseMoveEvent将如下所示:

void CustomTreeWidget::mouseMoveEvent(QMouseEvent *event)
{
    QTreeWidget::mouseMoveEvent(event);    
    QTreeWidgetItem *item = itemAt(event->pos();    
    if (item != NULL)
        qDebug(item->text(0).toLocal8Bit().data());
}

Please note the usage of QTableWidget::itemAt() function instead of childAt() . 请注意QTableWidget::itemAt()函数而不是childAt()childAt()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运算顺序int(4 * x / y)与int(x /(y / 4)) - Order of operation int(4*x/y) vs int(x/(y/4)) 为什么 int x{ y = 5 } 可能? - Why is int x{ y = 5 } possible? int x; int y; int * ptr; 是不是初始化,对吧? - int x; int y; int *ptr; is not initialization, right? 如何使用函数 gotoxy(int x, int y) - How to use function gotoxy(int x, int y) 为什么是((unsigned int)x)* y ==((unsigned int)(x * y)总是如此? - why is ((unsigned int)x) * y == ((unsigned int)(x * y) always true? (C ++内存管理)如果我们有一个int x = 1234,而int&y = x ...那么堆栈中y的地址是多少? - (C++ memory management) If we have an int x = 1234, and int &y = x… then what is the address of y on the stack? C ++ Stringstream int转换为字符串,但返回null - C++ Stringstream int to string but returns null C ++ 11 int x,int y之间的随机数,不包括列表<int> - C++11 Random between int x, int y, excluding list<int> 访问模板类A中的X和Y,如模板<template <int X,int Y> class> class A; - Accessing X and Y in template class A like in template<template<int X, int Y> class> class A; 如何让x和y从cvPoint分别与int进行协调? - How to get x and y cordinates separately to int from cvPoint?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM