QTreeWidget中怎么触发判断鼠标左键的点击事件
展开全部
1.继承QTreewidget重新实现他的mouseReleaseEvent方法,为什么是release方法,因为源码里面mousePressEvent方法用来绘制treeWidget,而release方法则会发送出点击事件的信号
#ifndefTREEWIDGET_H
#define TREEWIDGET_H
#ifndef TREEWIDGET_H
#define TREEWIDGET_H
#include <QWidget>
#include <QTreeWidget>
#include <QMouseEvent>
class QTreeWidget2 : public QTreeWidget
{
Q_OBJECT
public:
explicit QTreeWidget2( QWidget *parent = 0 );
protected:
void mouseReleaseEvent (QMouseEvent *event);
};
#endif // TREEWIDGET_H
2.实现
#include "treeWidget.h"
QTreeWidget2::QTreeWidget2( QWidget *parent)
:QTreeWidget(parent)
{
}
void QTreeWidget2::mouseReleaseEvent (QMouseEvent *event)
{
if (event->button () & Qt::RightButton )
{
emit customContextMenuRequested( event->pos ());
}else if (event->button () & Qt::LeftButton){
emit itemClicked(itemFromIndex (indexAt ( event->pos () )),indexAt ( event->pos () ).column ());
}
}
3.测试
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTreeWidgetItem>
class QTreeWidget2;
static const int itemtype1 = QTreeWidgetItem::Type;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow( QWidget *parent = 0 );
public slots:
void setTest2(const QPoint &pos);
void setTest(QTreeWidgetItem*item,int column);
private:
QTreeWidget2 *treeWidget;
QTreeWidgetItem *currentItem;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "treeWidget.h"
#include <QMessageBox>
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent)
{
treeWidget = new QTreeWidget2;
treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
treeWidget->setHeaderLabel ( tr("资源列表") );
QTreeWidgetItem *root = new QTreeWidgetItem( treeWidget );
root->setText (0,tr("root"));
QTreeWidgetItem *item = new QTreeWidgetItem( root );
item->setText (0,tr("a"));
QTreeWidgetItem *item1 = new QTreeWidgetItem( item );
item1->setText (0,tr("b"));
QTreeWidgetItem *item2 = new QTreeWidgetItem( item1 );
item2->setText (0,tr("c"));
connect( treeWidget,SIGNAL(customContextMenuRequested( const QPoint &)),
this,SLOT(setTest2(const QPoint &)));
connect( treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),
this,SLOT(setTest(QTreeWidgetItem*,int)));
setCentralWidget ( treeWidget );
resize ( 500,400 );
}
void MainWindow::setTest2(const QPoint &pos)
{
currentItem = treeWidget->itemAt(pos);
if ( currentItem == NULL )
return;
QMessageBox::warning (this,tr("test"),tr("hello"));
}
void MainWindow::setTest(QTreeWidgetItem*item,int column)
{
QMessageBox::warning (this,tr("asd"),tr("sd"));
#ifndefTREEWIDGET_H
#define TREEWIDGET_H
#ifndef TREEWIDGET_H
#define TREEWIDGET_H
#include <QWidget>
#include <QTreeWidget>
#include <QMouseEvent>
class QTreeWidget2 : public QTreeWidget
{
Q_OBJECT
public:
explicit QTreeWidget2( QWidget *parent = 0 );
protected:
void mouseReleaseEvent (QMouseEvent *event);
};
#endif // TREEWIDGET_H
2.实现
#include "treeWidget.h"
QTreeWidget2::QTreeWidget2( QWidget *parent)
:QTreeWidget(parent)
{
}
void QTreeWidget2::mouseReleaseEvent (QMouseEvent *event)
{
if (event->button () & Qt::RightButton )
{
emit customContextMenuRequested( event->pos ());
}else if (event->button () & Qt::LeftButton){
emit itemClicked(itemFromIndex (indexAt ( event->pos () )),indexAt ( event->pos () ).column ());
}
}
3.测试
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTreeWidgetItem>
class QTreeWidget2;
static const int itemtype1 = QTreeWidgetItem::Type;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow( QWidget *parent = 0 );
public slots:
void setTest2(const QPoint &pos);
void setTest(QTreeWidgetItem*item,int column);
private:
QTreeWidget2 *treeWidget;
QTreeWidgetItem *currentItem;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "treeWidget.h"
#include <QMessageBox>
#include <QtDebug>
MainWindow::MainWindow(QWidget *parent):
QMainWindow(parent)
{
treeWidget = new QTreeWidget2;
treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
treeWidget->setHeaderLabel ( tr("资源列表") );
QTreeWidgetItem *root = new QTreeWidgetItem( treeWidget );
root->setText (0,tr("root"));
QTreeWidgetItem *item = new QTreeWidgetItem( root );
item->setText (0,tr("a"));
QTreeWidgetItem *item1 = new QTreeWidgetItem( item );
item1->setText (0,tr("b"));
QTreeWidgetItem *item2 = new QTreeWidgetItem( item1 );
item2->setText (0,tr("c"));
connect( treeWidget,SIGNAL(customContextMenuRequested( const QPoint &)),
this,SLOT(setTest2(const QPoint &)));
connect( treeWidget,SIGNAL(itemClicked(QTreeWidgetItem*,int)),
this,SLOT(setTest(QTreeWidgetItem*,int)));
setCentralWidget ( treeWidget );
resize ( 500,400 );
}
void MainWindow::setTest2(const QPoint &pos)
{
currentItem = treeWidget->itemAt(pos);
if ( currentItem == NULL )
return;
QMessageBox::warning (this,tr("test"),tr("hello"));
}
void MainWindow::setTest(QTreeWidgetItem*item,int column)
{
QMessageBox::warning (this,tr("asd"),tr("sd"));
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询