Hi everyone,
I have a QTabelView with a few rows and I would like to make a Popup menu that when I selectm for example the first item in the menu, that shows me the value of the first cell in the current row.
Printable View
Hi everyone,
I have a QTabelView with a few rows and I would like to make a Popup menu that when I selectm for example the first item in the menu, that shows me the value of the first cell in the current row.
Good for you!
http://catb.org/~esr/faqs/smart-questions.html
I understand the words. I don't understand why do you expect others to guess what you are having problems with. So far you told us what you were working on, not more. So I replied that I was very happy for you that you had something to fill your time with.
On the other hand if you want to ask for something, spare 30 minutes and read through the text behind the link I gave you (pay special attention to the "Be precise and informative about your problem" and "Questions Not To Ask" parts) and then restate your problem.
Yes you are right.
So, I've recasted my problem.
How to make a Popup menu which is connected to TableView and when I press one item on menu displays the value of the first cell in selected row?
So, I've two problems:
1. How to create a Popup menu connected to TableView?
2. How do display the value o some cell in selected row.
Forgive me, I did't want to do anyone a clairvoyant.
There are basically three ways to do that. Take a look at QWidget::contextMenuPolicy property to see description of all of them.
Use QModelIndex::data() to retrieve the value of an index of a table.Quote:
2. How do display the value o some cell in selected row.
Thanks,
I've made:
but, unfortunately, when I'm pressing a right mouse button any menu does not appear :/Code:
TableResult->setRowCount(3); TableResult->setColumnCount(5); TableResult->setContextMenuPolicy(Qt::ActionsContextMenu); newAct->setStatusTip(tr("Create a new file")); connect(newAct, SIGNAL(triggered()), this, SLOT(newFile())); menu.addAction(newAct);
It doesn't work that way. You have to add the action to the widget, not to some temporary menu object.
Ok, now I've:
Code:
newAct->setStatusTip(tr("Create a new file")); TableResult->addAction(newAct); TableResult->setContextMenuPolicy(Qt::ActionsContextMenu); connect(newAct, SIGNAL(triggered()),TableResult,SLOT(newFile()));
and
of course I've in header file:
Code:
{ Q_OBJECT public: ~MainWindow(); QAction *newAct; QTableWidget *TableResult; private slots: void newFile(); private: int t; };
but, menu appears, but when I click on "new file" nothing happens :/ Method MainWindow::newFile will not start
You connect the signal to a slot in the table, not in the main window.Code:
connect(newAct, SIGNAL(triggered()),TableResult,SLOT(newFile()));
Thank you for your quick response and for your patience, but how can I connect the signal to TableView?
And second question, how can I read a value of selected (by showing menu) row, you've wrote by "QModelIndex::data()" but in newFile() funcition I haven't data :/
Are you asking me about the prototype of QObject::connect() or something else?
If you have the selected row, you have its index. When you have its index, you can call data() on it.Quote:
... but in newFile() funcition I haven't data :/
You have declared the slot in your MainWindow Class, but connected the Signal to your TreeWidget. So, to connect to your newFile() Slot, your connect would have to look like this:
Take a look at the link wysota posted if you want to know more about this.Code:
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
As you're using a TableWidget, not a TableView, simply call
Or, if you need data from another column in the rowCode:
TableResult->currentItem()->data(...)
If you want to use a TableView, you'll have to work with indexes, of course, but that's pretty much the same.Code:
TableResult->item(TableResult->currentRow(), your Column)->data(...)
Thanks a lot!
I's working!