please help me, how to get data from selected row from QTableWidget, I try many forms but cannot understand how to do.
some body have any sample?
tank you very much.
Batista
please help me, how to get data from selected row from QTableWidget, I try many forms but cannot understand how to do.
some body have any sample?
tank you very much.
Batista
same problem here!
did you find any useful link?!
One answer could be the item(...) member function which returns a QTableWidgetItem, and the item has a text() member function which returns a QString.
The answer can depend on how you use that classes in your project (it makes sense to inherit from those classes in some projects)
I have similar problem.
I have a QTableWidget *table and I have to read data from it's item when user change it's data manually.
I use this code:
where MySlot have to do something like this:Qt Code:
To copy to clipboard, switch view to plain text mode
What should I do to define the item for MySlot?
I used the example function text(int,int) from C++ GUI Programming with Qt 4 book.
Qt Code:
void MyWidget::doCalculate() { action->setText(dataSheet->text(0,1)); } { Cell *c = cell(row, column); if (c) { return c->text(); } else { return ""; } }To copy to clipboard, switch view to plain text mode
My problem is that a specific column has a QComboBox...how can I get the value of the QComboBox?
The text(int,int) function definitely does not work.
Any ideas?!
@mandarinka: The code should be something like: connect(table,SIGNAL(itemChanged(QTableWidgetItem* )),this,SLOT(MySlot(QTableWidgetItem*))); //and take the pointer as parameter to your slot.
@fatecasino: see QComboBox documentation, itemText() or itemData(), depending on your needs.
I tried this, but i crashes..
it only works if the item(1,0) is an empty cell.Qt Code:
c = dataSheet->item(1,0);//it is a QComboBox action->setText(c->text());//it is a QLineEditTo copy to clipboard, switch view to plain text mode
I find it difficult to get the values from QComboBox when it is in a specific position in the QTableWidget
Last edited by fatecasino; 15th February 2011 at 16:40. Reason: updated contents
Well QComboBox has: itemText() or itemData() member functions to return the value.
From your little code snippet i didn't understand exactly what are you trying to achieve and what have you done so far (have you inherited from QTableWidgetItem to use the QComboBox?)... so post a small compilable example.
Qt Code:
c = dataSheet->item(1,0);//it is a QComboBox action->setText(c->text());//it is a QLineEditTo copy to clipboard, switch view to plain text mode
Why are you creating a new QTableWidgetItem here, and then immediately overwriting the pointer with a different pointer from your table? That's a memory leak, and if you have never set a QTableWidgetItem for the cell, then item() returns a NULL pointer. Trying to call c->text() when c is a NULL pointer will of course crash.
In general, if you want to retrieve a QTableWidgetItem from a table, you just do this:
Qt Code:
To copy to clipboard, switch view to plain text mode
But that -isn't- what you want if you have installed a custom QWidget into a table cell. The QTableWidgetItem and the custom QWidget are basically two GUI things that occupy the same space at the same time. (If you set the background of your custom QWidget to something transparent, you can see that the QTableWidgetItem text is drawn under the custom QWidget).
If you are setting the custom QWidget through a call to QTableWidget::setCellWidget(), then you need to retrieve the custom QWidget through the QTableView::cellWidget() method.
I do not know if changing the selection in a combo box that is a cell widget in a table results in the itemChanged() signal being emitted. Since you can put -any- QWidget into a table cell, it is hard to imagine a general implementation that the table widget could use. I think that if you are using the combo box as a cell widget, then you need to connect a slot to the combo box widget's currentIndexChanged() signal to get a notification that the user has selected something new.
well, let me explain.
I have used the C++ GUI Programming with Qt 4 book example as a start, overloading the QTableWidget:
Qt Code:
#include <QTableWidget> #include <QComboBox> #include <myTableItem.h> { public: ActionCombo(): { setMaxCount(3); addItem("absorb"); addItem("emit"); addItem("emit i=0"); } }; { public: DistributionCombo(): { setMaxCount(5); addItem("gauss"); addItem("rotate"); addItem("voigt"); addItem("rot&gauss"); addItem("lorentz"); } }; { public: AdjustCombo(): { setMaxCount(2); addItem("normal"); addItem("adjust"); } }; class Cell; { Q_OBJECT public: bool autoRecalculate() const { return autoRecalc; } void clear(); Cell *cell(int row, int column) const; ActionCombo* actionCombo; public slots: void setRowSelected(int); void appendRow(); private slots: void somethingChanged(); .......... signals: void modified(); private: int getSelectedRow(); int selectedRow; DistributionCombo* distributionCombo; AdjustCombo* adjustCombo; enum { MagicNumber = 0x7F51C883, RowCount = 1, ColumnCount = 7 }; bool autoRecalc; };To copy to clipboard, switch view to plain text mode
I construct a MyTable like this:
Qt Code:
{ autoRecalc = true; setItemPrototype(new Cell); setSelectionMode(ContiguousSelection); this, SLOT(somethingChanged())); clear(); } void MyTable::clear() { setRowCount(0); setColumnCount(0); setRowCount(RowCount); setColumnCount(ColumnCount); QStringList list; list<<"action"<<"distribution"<<"AL,V,width"<<"Voigt or MixtG"<<"Xi"<<"AL-left_right"<<"adjust"; setHorizontalHeaderLabels(list); actionCombo = new ActionCombo(); distributionCombo = new DistributionCombo(); adjustCombo = new AdjustCombo() ; setCellWidget(0,0,actionCombo); setCellWidget(0,1,distributionCombo); setCellWidget(0,6,adjustCombo); setCurrentCell(0, 0); } void MyTable::appendRow()//this works but perhaps it 's not correct!! { int currentRows = rowCount(); insertRow(currentRows); ActionCombo* actionCombo2 = new ActionCombo(); DistributionCombo* distributionCombo2 = new DistributionCombo(); AdjustCombo* adjustCombo2 = new AdjustCombo() ; setCellWidget(currentRows,0,actionCombo2); setCellWidget(currentRows,1,distributionCombo2); setCellWidget(currentRows,6,adjustCombo2); setCurrentItem(actionCombo2); }To copy to clipboard, switch view to plain text mode
Hence,
I have a MyTable, the user types in some data and uses the QComboBoxes to set some values.
At the end of each row, s/he pushes the addButton and calls the appendRow() function to create a new row of similar items (3 combos, 4 empty).
Now, I simply want to collect the data that the user inputs. How can I get the value of the QComboBox located at (5,1) for instance?
Let me offer my design philosophy.
I believe that if you require to extract information from a UI element (ie, it is not write only) then that UI element should be a part of a MVC pattern.
For example, QTableWidget is fine to just show the user some quick information, but if you require more than that, then you should use a model-based approach and thus store the data yourself. It is much easier than trying to get the data back from another system (QTableWidget in this case) and parsing it.
So, in this case, you would already have the data, as the UI will have fed you the data as it was modified, as you are the data store. Much easier.
fatecasino (15th February 2011)
@squidge - I agree, model / view is much more appropriate for this (and for most other cases where there is a data collection behind a table, IMO).
@fatecasino: However, if you really want to use a QTableWidget instead of a QTableView and model:
1 - you need to create a connection from the QTableWidget's cellClicked() signal to a slot so you know -which- combobox is being edited.
2 - you need to connect each combobox's currentIndexChanged() signal to a slot so you can tell when the user has made a new selection.
3 - the combination of these first two signals will tell you (a) which combox box is in use and (b) what selection the user made.
4 - when the user clicks the Add button, you can do one of the following:
(a) Read each cell in the current row and retrieve either its text (if it is an ordinary QTableWidgetItem that-you- have created and put there) or retrieve the widget (using cellWidget()) and then use that pointer to read the selected text.
(b) Store the row information somewhere in your application external to the table, and update it in response to combobox currentIndexChanged () or table widget itemChanged() signals. Use this to populate the new row in the table.
But as squidge suggested, you're doing this the hard way. Read up on model / view (not just the class documentation, but read the tutorial stuff too).
fatecasino (15th February 2011)
thanks to both of you!
The last few hours I have been studying this famous article:
http://doc.trolltech.com/4.7.old/mod...ogramming.html
It looks very analytic and well-written, I have understood the theory part, BUT when I want to start writing a simple example I get totally lost.There are so many different things to arrange. I will give it 2-3 more days of study, I think it 's a very important concept.
Have you tried typecasting the QTableWidgetItem cells you know will be a QComboBox to QComboBox?
ie.
Qt Code:
{ if(ComboBoxcolumn_cell != NULL) { ComboBoxcolumn_cell->currentText(); } }To copy to clipboard, switch view to plain text mode
BTW I'm new to this forum, how do you indent code? The indent tags don't work for me
Last edited by positive_4_life; 16th February 2011 at 16:04.
This is guaranteed to fail. A QTableWidgetItem is totally unrelated to a QComboBox. QTableWidgetItem isn't even derived from QWidget -or- QObject, so what makes you think a qobject_cast<> would even compile? Casting isn't like alchemy - it can't turn straw into gold unless the straw was golden to begin with.Have you tried typecasting the QTableWidgetItem cells you know will be a QComboBox to QComboBox?
As I said in a previous reply in this thread, just because you've put a widget into a cell in a table, it -does not- mean that it has replaced the QTableWidgetItem in that cell (if there was one there in the first place, which it won't be unless -you- put it there). A QTableWidgetItem and a cell QWidget both occupy the same cell at the same time. It's a clear violation of the laws of physics, but it's OK, this is software.
If you've put a TableWidgetItem into a cell, you get it by calling the table's item() method. If you put a QWidget into a cell, you get it by calling the table's cellWidget() method.
positive_4_life (16th February 2011)
I think the comment above is one of the most useful! I had not realize it AND I don't think it is clearly written in any post.just because you've put a widget into a cell in a table, it -does not- mean that it has replaced the QTableWidgetItem in that cell (if there was one there in the first place, which it won't be unless -you- put it there). A QTableWidgetItem and a cell QWidget both occupy the same cell at the same time.
Now, I am still struggling with the model-view idea. It's a new thing to me.
I am at the 80% of my project and I will try to do it the hard way (while trying to understand the model-view)
I tried it; it works for all the empty cells but it doesn't work for the combobox!1 - you need to create a connection from the QTableWidget's cellClicked() signal to a slot so you know -which- combobox is being edited.
Qt Code:
connect(table,SIGNAL(cellClicked(int,int)), this,SLOT(setCurrentPosition(int,int)) );To copy to clipboard, switch view to plain text mode
It works:2 - you need to connect each combobox's currentIndexChanged() signal to a slot so you can tell when the user has made a new selection.
Qt Code:
To copy to clipboard, switch view to plain text mode
Any ideas how to make step 1 to work? I tried every single QTableWidget SIGNAL, but none of them works for the combobox..is it the end of the "hard way"?!
Bookmarks