[英]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.