Right Click on a cell to copy the content of it
-
manager is the cpp file name. I use this right click event in manager cpp code. So this is the file name..
for example, I use cellClicked function which belongs to QTableWidget;
connect(ui->tableWidget, &QTableWidget::cellClicked, this, &manager::cellClicked);
If I click the cell, function do some jobs in that cell..as you can see, manager is the file name.
Whatever, I can use it for the one click. Thats ok. I want to handle right click.@hubeytqew
Using a context menu should work:ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->tableWidget, &QWidget::customContextMenuRequested,this,[this] () { QList<QTableWidgetItem *> items=ui->tableWidget->selectedItems(); if(items.count()>0) qDebug()<<items[0]->text(); });
-
I have a table created with QTableWidget. I want to copy the cell's content (data) with right click. I've searched the web but haven't found anything that works for the right click event. How can I achieve this?
@hubeytqew said in Right Click on a cell to copy the content of it:
I've searched the web but haven't found anything that works for the right click event.
I don't know whether this is the best or only way, but every widget has a
mousePressEvent(QMouseEvent *event)
. Overriding that you can inspect whether it's a right button andQTableWidgetItem *QTableWidget::itemAt(const QPoint &point) const
can tell you which item it's on. -
@hubeytqew said in Right Click on a cell to copy the content of it:
I've searched the web but haven't found anything that works for the right click event.
I don't know whether this is the best or only way, but every widget has a
mousePressEvent(QMouseEvent *event)
. Overriding that you can inspect whether it's a right button andQTableWidgetItem *QTableWidget::itemAt(const QPoint &point) const
can tell you which item it's on.@JonB
Now, I catch right click in the app with below code.void manager::mousePressEvent(QMouseEvent *e) { if (e->button() == Qt::RightButton) { qDebug() << "hi!"; } }
But it can not catch right click on the QTableWidget. How do I catch the right click on the table?
-
@JonB
Now, I catch right click in the app with below code.void manager::mousePressEvent(QMouseEvent *e) { if (e->button() == Qt::RightButton) { qDebug() << "hi!"; } }
But it can not catch right click on the QTableWidget. How do I catch the right click on the table?
@hubeytqew
By doing this on theQTableWidget
? I have no idea whatmanager
might be.... -
@hubeytqew
By doing this on theQTableWidget
? I have no idea whatmanager
might be....manager is the cpp file name. I use this right click event in manager cpp code. So this is the file name..
for example, I use cellClicked function which belongs to QTableWidget;
connect(ui->tableWidget, &QTableWidget::cellClicked, this, &manager::cellClicked);
If I click the cell, function do some jobs in that cell..as you can see, manager is the file name.
Whatever, I can use it for the one click. Thats ok. I want to handle right click. -
manager is the cpp file name. I use this right click event in manager cpp code. So this is the file name..
for example, I use cellClicked function which belongs to QTableWidget;
connect(ui->tableWidget, &QTableWidget::cellClicked, this, &manager::cellClicked);
If I click the cell, function do some jobs in that cell..as you can see, manager is the file name.
Whatever, I can use it for the one click. Thats ok. I want to handle right click.@hubeytqew
You want to know about mouse presses on theQTableWidget
, right? Somanager::mousePressEvent()
onmanager
is not the right thing.You need to subclass Qt's
QTableWidget
to produce your own derived class and then in that you canoverride
themousePressEvent()
method like you showed --- but not on whatevermanager
is. And you will not be using signals & slots here, only overridingmousePressEvent()
. I don't think you can use any of theclicked
signals as I believe they are only generated for left-click, not right-click? -
clicked() is only emitted for Qt::LeftButton, pressed() for all buttons. activated() is emitted for all buttons when the style hint SH_ItemView_ActivateItemOnSingleClick is enabled.
-
clicked() is only emitted for Qt::LeftButton, pressed() for all buttons. activated() is emitted for all buttons when the style hint SH_ItemView_ActivateItemOnSingleClick is enabled.
@Christian-Ehrlicher , @hubeytqew
Which I think implies you will indeed need to go for overriding themousePressEvent()
as suggested previously.BTW, if you don't want to subclass and override, I presume you could instead use void QObject::installEventFilter(QObject *filterObj) and get a mouse press event where you can test for right button, though I haven't tested.
-
manager is the cpp file name. I use this right click event in manager cpp code. So this is the file name..
for example, I use cellClicked function which belongs to QTableWidget;
connect(ui->tableWidget, &QTableWidget::cellClicked, this, &manager::cellClicked);
If I click the cell, function do some jobs in that cell..as you can see, manager is the file name.
Whatever, I can use it for the one click. Thats ok. I want to handle right click.@hubeytqew
Using a context menu should work:ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->tableWidget, &QWidget::customContextMenuRequested,this,[this] () { QList<QTableWidgetItem *> items=ui->tableWidget->selectedItems(); if(items.count()>0) qDebug()<<items[0]->text(); });
-
@hubeytqew
Using a context menu should work:ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->tableWidget, &QWidget::customContextMenuRequested,this,[this] () { QList<QTableWidgetItem *> items=ui->tableWidget->selectedItems(); if(items.count()>0) qDebug()<<items[0]->text(); });
-
@mpergand
I agree this is nice "simple" code, but it has flaws, does it not, for "Right Click on a cell to copy the content of it"? Examples: table widget set to not allow selection or set to allow multi-cell selection? -
@hubeytqew
Using a context menu should work:ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->tableWidget, &QWidget::customContextMenuRequested,this,[this] () { QList<QTableWidgetItem *> items=ui->tableWidget->selectedItems(); if(items.count()>0) qDebug()<<items[0]->text(); });
-
bro, you are just perfect....
This code is working without any correction...only the thing is I should copy the text to clipboard. I'll find it, I think.
@hubeytqew said in Right Click on a cell to copy the content of it:
copy the text to clipboard.
qApp->clipboard()->setText("you are just perfect....");
:) -
@hubeytqew said in Right Click on a cell to copy the content of it:
copy the text to clipboard.
qApp->clipboard()->setText("you are just perfect....");
:)