PDA

View Full Version : Popup menu in TabelView on one row



TomASS
26th September 2009, 20:53
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.

wysota
26th September 2009, 22:57
Good for you!

http://catb.org/~esr/faqs/smart-questions.html

TomASS
26th September 2009, 23:14
Good for you!

http://catb.org/~esr/faqs/smart-questions.html


I'm sorry for my english - whose words don't you understand, I'll try to explain more.

wysota
26th September 2009, 23:43
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.

TomASS
27th September 2009, 11:15
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.

wysota
27th September 2009, 12:52
1. How to create a Popup menu connected to TableView?
There are basically three ways to do that. Take a look at QWidget::contextMenuPolicy property to see description of all of them.


2. How do display the value o some cell in selected row.

Use QModelIndex::data() to retrieve the value of an index of a table.

TomASS
27th September 2009, 20:35
Thanks,

I've made:


TableResult = new QTableWidget;
TableResult->setRowCount(3);
TableResult->setColumnCount(5);
TableResult->setContextMenuPolicy(Qt::ActionsContextMenu);

newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));

QMenu menu(TableResult);
menu.addAction(newAct);
but, unfortunately, when I'm pressing a right mouse button any menu does not appear :/

wysota
27th September 2009, 20:44
It doesn't work that way. You have to add the action to the widget, not to some temporary menu object.

TomASS
27th September 2009, 20:52
Ok, now I've:


newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Create a new file"));
TableResult->addAction(newAct);
TableResult->setContextMenuPolicy(Qt::ActionsContextMenu);
connect(newAct, SIGNAL(triggered()),TableResult,SLOT(newFile()));

and

void MainWindow::newFile(){
QMessageBox::critical(0,"ttt1","ttt2");
}

of course I've in header file:

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~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

wysota
27th September 2009, 21:02
connect(newAct, SIGNAL(triggered()),TableResult,SLOT(newFile()));
You connect the signal to a slot in the table, not in the main window.

TomASS
27th September 2009, 21:09
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 :/

wysota
27th September 2009, 23:36
Thank you for your quick response and for your patience, but how can I connect the signal to TableView?

Are you asking me about the prototype of QObject::connect() or something else?


... but in newFile() funcition I haven't data :/

If you have the selected row, you have its index. When you have its index, you can call data() on it.

ChiliPalmer
28th September 2009, 01:39
Thank you for your quick response and for your patience, but how can I connect the signal to TableView?

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:

connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
Take a look at the link wysota posted if you want to know more about this.


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 :/
As you're using a TableWidget, not a TableView, simply call

TableResult->currentItem()->data(...)
Or, if you need data from another column in the row

TableResult->item(TableResult->currentRow(), your Column)->data(...)
If you want to use a TableView, you'll have to work with indexes, of course, but that's pretty much the same.

TomASS
28th September 2009, 08:08
Thanks a lot!

I's working!