Results 1 to 16 of 16

Thread: need get data from selected row from QTableWidget?

  1. #1
    Join Date
    Nov 2010
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Symbian S60

    Default need get data from selected row from QTableWidget?

    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

  2. #2
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need get data from selected row from QTableWidget?

    same problem here!
    did you find any useful link?!

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: need get data from selected row from QTableWidget?

    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)

  4. #4
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    1
    Qt products
    Qt4

    Default Re: need get data from selected row from QTableWidget?

    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:
    Qt Code:
    1. connect(table,SIGNAL(itemChanged(QTableWidgetItem*)),this,SLOT(MySlot()));
    To copy to clipboard, switch view to plain text mode 
    where MySlot have to do something like this:
    Qt Code:
    1. QString str;
    2. str = item->text();
    To copy to clipboard, switch view to plain text mode 

    What should I do to define the item for MySlot?

  5. #5
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need get data from selected row from QTableWidget?

    I used the example function text(int,int) from C++ GUI Programming with Qt 4 book.

    Qt Code:
    1. void MyWidget::doCalculate()
    2. {
    3.  
    4. action->setText(dataSheet->text(0,1));
    5. }
    6.  
    7. QString MyTable::text(int row, int column) const
    8. {
    9. Cell *c = cell(row, column);
    10. if (c) {
    11. return c->text();
    12. } else {
    13. return "";
    14. }
    15. }
    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?!

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: need get data from selected row from QTableWidget?

    @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.

  7. #7
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need get data from selected row from QTableWidget?

    I tried this, but i crashes..

    Qt Code:
    1. c = dataSheet->item(1,0);//it is a QComboBox
    2. action->setText(c->text());//it is a QLineEdit
    To copy to clipboard, switch view to plain text mode 
    it only works if the item(1,0) is an empty cell.
    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

  8. #8
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: need get data from selected row from QTableWidget?

    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.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: need get data from selected row from QTableWidget?

    Qt Code:
    1. c = dataSheet->item(1,0);//it is a QComboBox
    2. action->setText(c->text());//it is a QLineEdit
    To 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:
    1. QTableWidgetItem * c = dataSheet->item(1,0);
    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.

  10. #10
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need get data from selected row from QTableWidget?

    well, let me explain.
    I have used the C++ GUI Programming with Qt 4 book example as a start, overloading the QTableWidget:

    Qt Code:
    1. #include <QTableWidget>
    2. #include <QComboBox>
    3. #include <myTableItem.h>
    4.  
    5. class ActionCombo : public QComboBox
    6. {
    7. public:
    8. ActionCombo():
    9. {
    10. setMaxCount(3);
    11. addItem("absorb");
    12. addItem("emit");
    13. addItem("emit i=0");
    14. }
    15. };
    16.  
    17. class DistributionCombo : public QComboBox
    18. {
    19. public:
    20. DistributionCombo():
    21. {
    22. setMaxCount(5);
    23. addItem("gauss");
    24. addItem("rotate");
    25. addItem("voigt");
    26. addItem("rot&gauss");
    27. addItem("lorentz");
    28. }
    29. };
    30.  
    31. class AdjustCombo : public QComboBox
    32. {
    33. public:
    34. AdjustCombo():
    35. {
    36. setMaxCount(2);
    37. addItem("normal");
    38. addItem("adjust");
    39. }
    40. };
    41. class Cell;
    42. class MyTable : public QTableWidget
    43. {
    44. Q_OBJECT
    45.  
    46. public:
    47.  
    48. MyTable(QWidget *parent = 0);
    49. bool autoRecalculate() const { return autoRecalc; }
    50. QString currentLocation() const;
    51. QString currentFormula() const;
    52. QTableWidgetSelectionRange selectedRange() const;
    53. void clear();
    54. bool readFile(const QString &fileName);
    55. bool writeFile(const QString &fileName);
    56. Cell *cell(int row, int column) const;
    57. QString text(int row, int column) const;
    58. ActionCombo* actionCombo;
    59. QString formula(int row, int column) const;
    60. public slots:
    61. void setRowSelected(int);
    62. void appendRow();
    63. private slots:
    64. void somethingChanged();
    65. ..........
    66.  
    67. signals:
    68. void modified();
    69. private:
    70. int getSelectedRow();
    71. int selectedRow;
    72. DistributionCombo* distributionCombo;
    73. AdjustCombo* adjustCombo;
    74. enum { MagicNumber = 0x7F51C883, RowCount = 1, ColumnCount = 7 };
    75. void setFormula(int row, int column, const QString &formula);
    76.  
    77. bool autoRecalc;
    78. };
    To copy to clipboard, switch view to plain text mode 

    I construct a MyTable like this:
    Qt Code:
    1. MyTable ::MyTable (QWidget *parent)
    2. : QTableWidget(parent)
    3. {
    4.  
    5. autoRecalc = true;
    6. setItemPrototype(new Cell);
    7. setSelectionMode(ContiguousSelection);
    8. connect(this, SIGNAL(itemChanged(QTableWidgetItem *)),
    9. this, SLOT(somethingChanged()));
    10. clear();
    11.  
    12. }
    13. void MyTable::clear()
    14. {
    15. setRowCount(0);
    16. setColumnCount(0);
    17. setRowCount(RowCount);
    18. setColumnCount(ColumnCount);
    19.  
    20. list<<"action"<<"distribution"<<"AL,V,width"<<"Voigt or MixtG"<<"Xi"<<"AL-left_right"<<"adjust";
    21. setHorizontalHeaderLabels(list);
    22. actionCombo = new ActionCombo();
    23. distributionCombo = new DistributionCombo();
    24. adjustCombo = new AdjustCombo() ;
    25.  
    26. setCellWidget(0,0,actionCombo);
    27. setCellWidget(0,1,distributionCombo);
    28. setCellWidget(0,6,adjustCombo);
    29.  
    30. setCurrentCell(0, 0);
    31. }
    32. void MyTable::appendRow()//this works but perhaps it 's not correct!!
    33. {
    34. int currentRows = rowCount();
    35. insertRow(currentRows);
    36.  
    37. ActionCombo* actionCombo2 = new ActionCombo();
    38. DistributionCombo* distributionCombo2 = new DistributionCombo();
    39. AdjustCombo* adjustCombo2 = new AdjustCombo() ;
    40.  
    41. setCellWidget(currentRows,0,actionCombo2);
    42. setCellWidget(currentRows,1,distributionCombo2);
    43. setCellWidget(currentRows,6,adjustCombo2);
    44. setCurrentItem(actionCombo2);
    45. }
    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?

  11. #11
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: need get data from selected row from QTableWidget?

    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.

  12. The following user says thank you to squidge for this useful post:

    fatecasino (15th February 2011)

  13. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: need get data from selected row from QTableWidget?

    @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).

  14. The following user says thank you to d_stranz for this useful post:

    fatecasino (15th February 2011)

  15. #13
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need get data from selected row from QTableWidget?

    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.

  16. #14
    Join Date
    Nov 2009
    Posts
    14
    Thanks
    4

    Default Re: need get data from selected row from QTableWidget?

    Quote Originally Posted by fatecasino View Post
    I used the example function text(int,int) from C++ GUI Programming with Qt 4 book.

    Qt Code:
    1. void MyWidget::doCalculate()
    2. {
    3.  
    4. action->setText(dataSheet->text(0,1));
    5. }
    6.  
    7. QString MyTable::text(int row, int column) const
    8. {
    9. Cell *c = cell(row, column);
    10. if (c) {
    11. return c->text();
    12. } else {
    13. return "";
    14. }
    15. }
    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?!
    Have you tried typecasting the QTableWidgetItem cells you know will be a QComboBox to QComboBox?

    ie.

    Qt Code:
    1. void mySlot(QTableWidgetItem* cell)
    2. {
    3. QComboBox* ComboBoxcolumn_cell = qobject_cast<QComboBox*>(cell);
    4. if(ComboBoxcolumn_cell != NULL)
    5. {
    6. ComboBoxcolumn_cell->currentText();
    7. }
    8. }
    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.

  17. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: need get data from selected row from QTableWidget?

    Have you tried typecasting the QTableWidgetItem cells you know will be a QComboBox to QComboBox?
    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.

    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.

  18. The following user says thank you to d_stranz for this useful post:

    positive_4_life (16th February 2011)

  19. #16
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: need get data from selected row from QTableWidget?

    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.
    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.


    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)

    1 - you need to create a connection from the QTableWidget's cellClicked() signal to a slot so you know -which- combobox is being edited.
    I tried it; it works for all the empty cells but it doesn't work for the combobox!
    Qt Code:
    1. connect(table,SIGNAL(cellClicked(int,int)), this,SLOT(setCurrentPosition(int,int)) );
    To copy to clipboard, switch view to plain text mode 


    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.
    It works:
    Qt Code:
    1. connect(combo,SIGNAL(currentIndexChanged(QString)),info,SLOT(setText(QString)) );
    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"?!

Similar Threads

  1. Border around selected cell in qtablewidget...
    By pyqt123 in forum Qt Programming
    Replies: 4
    Last Post: 16th July 2012, 13:55
  2. Selected Rows moveUp/moveDown in QTableWidget
    By imagineryhead in forum Qt Programming
    Replies: 12
    Last Post: 14th September 2010, 15:01
  3. QTableView only showing data when row selected
    By Banjo in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2009, 05:34
  4. Replies: 5
    Last Post: 2nd April 2007, 08:57
  5. Replies: 5
    Last Post: 27th May 2006, 13:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.