在mouseEvent
上获取QTableWidget
时出现问题,此代码是创建一个tabelwidget
和mouseclickevent
的窗口,当我点击鼠标右键然后我有两个动作名为"的事件选项添加"和"删除",
当我点击"添加"我想添加3列的新行事件功能,当我点击&#34时删除最后一行;删除"事件功能,
(对不起我的英文),感谢任何帮助。
#include "notepad.h"
#include <QMessageBox>
#include <QTableView>
#include <QMouseEvent>
Notepad::Notepad()
{
test() ;
add_action = new QAction(tr("Add cell"), this);
add_action->setIcon(QIcon("add.jpg"));
Delete_action = new QAction(tr("Delete cell"), this);
Delete_action->setIcon(QIcon("delete.jpg"));
connect(Delete_action, SIGNAL(triggered()), this, SLOT(Delete()));
connect(add_action, SIGNAL(triggered()), this, SLOT(add()));
centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
centralWidget()->setAttribute(Qt::WA_MouseTracking,true);
setMouseTracking(true);
}
void Notepad::test()
{
QTableWidget* table = new QTableWidget();
QTableWidgetItem* tableItem = new QTableWidgetItem();
table->setRowCount(1);
table->setColumnCount(3);
table->setItem(0,0,new QTableWidgetItem());
table->setItem(0,1,new QTableWidgetItem());
table->setItem(0,2,new QTableWidgetItem());
table->setMouseTracking(true);
table->viewport()->setMouseTracking(true);
table->installEventFilter(this);
table->viewport()->installEventFilter(this);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->setSelectionMode(QAbstractItemView::SingleSelection);
tableItem->setFlags(tableItem->flags() ^ Qt::ItemIsEditable);
table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
setCentralWidget(table);
}
void Notepad::mouseReleaseEvent (QMouseEvent * event )
{
QMessageBox* msgBox;
if(event->button() == Qt::RightButton)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
QMenu *menu = new QMenu(this);
menu->addAction(add_action);
menu->addAction(Delete_action);
menu->exec(mouseEvent->globalPos());
}
}
void Notepad::add()
{
QTableWidget* table = new QTableWidget();
test();
table->setColumnCount(3);*/
int newRow = table->rowCount();
int newcol = table->columnCount();
qDebug() << newRow;
for (int row ; row < newRow+1 ; ++row)
{
QWidget *parent;
QStyleOptionViewItem option;
for (int column = 0; column < 3; ++column)
{
table->insertRow( table->rowCount());
table->insertColumn( newcol );
}
}
setCentralWidget(table);
centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
setMouseTracking(true);
}
void Notepad::Delete()
{
QTableWidget* table = new QTableWidget();
add();
int row=table->rowCount();
if (int i= row){
table->removeRow(i);
}
setCentralWidget(table);
centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
setMouseTracking(true);
}
答案 0 :(得分:0)
您有两个问题:如何创建上下文菜单以及如何添加行。
第1步:创建一次菜单。例如,在构造函数中。
// _menu is a class member.
_menu = new QMenu(this);
_menu->addAction(add_action);
_menu->addAction(Delete_action);
第2步:在相应的事件中显示:
void Notepad::contextMenuEvent(QContextMenuEvent *event)
{
_menu->exec(event->globalPos());
}
首先,您需要一个表作为类成员。为什么每次都要创建一个新实例?它们是不同的对象!您可以在每个add
电话上创建一个新的表格小部件!
第二,您需要增加行数并填充新行:
void Notepad::add()
{
const int newIndex = _table->rowCount();
_table->setRowCount(newIndex + 1);
// Fill data in row with index newIndex.
...
}