Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: QTableWidget

  1. #1
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy QTableWidget

    hey there i am trying to get values from my table entered by the user - but i am still having trouble getting the users data. my table is initially filled with all zero's but when the user enters their own number the only number that i get back is the original number which was zero. ive tried using teh following code to get a number from a particluar cell but i only get back zero?? any idea's....

    int multiply = demands->item(2,1)->data(Qt:isplayRole).toInt();

    demands is the name of my table and i just want the number the user enters at (2,1) to be stored inside multiply.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget

    What does this output?
    Qt Code:
    1. qDebug() << demands->item(2,1)->data(Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    (you need to add #include <QtDebug> on the top of your file and "CONFIG += debug" to .pro file)

  3. #3
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    i tried adding in that line - where should the output from qDebug appear?? i put that line of code in the same function as where the table got created - is that the correct place to put that piece of code? or should i create a seperate function, like a slot or something??

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget

    How about this:

    QTableWidgetItem::text()

  5. #5
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: QTableWidget

    i cant get qDebug to output anywhere - i dont see it...is it meant to appear onto the command prompt??

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget

    Quote Originally Posted by therealjag
    i tried adding in that line - where should the output from qDebug appear??
    On the console, if you use windows, you have to add "CONFIG += console" to your .pro file.

    i put that line of code in the same function as where the table got created - is that the correct place to put that piece of code? or should i create a seperate function, like a slot or something??
    Put it just above the "int multiply = ..." line.

  7. #7
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Still stuck with QTableWidget

    hey there i am still trying to obtain the users data from my QTableWidget. i tried using the following line

    int multiply = demands->item(2,1)->data(Qt:isplayRole).toInt();

    but it only gives the data that was originally there before the user enters the new data and not afterwards...do i have to add an event filter of some kind so that i only record the input - once the user has clicked on a cell and modifed it???

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Still stuck with QTableWidget

    How do you change the value in your model? If data() retrieves an "old" value, it means there is no "new" value. It looks like editing the item failed or was not completed. Could you please show the editing code?

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Still stuck with QTableWidget

    Please, don't start more than one thread on the same problem.

  10. #10
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    oh rite ok heres my code for that tab then which the table is inserted in:

    Qt Code:
    1. DemandsTab:DemandsTab(const QFileInfo &fileInfo, QWidget *parent)
    2. : QWidget(parent)
    3. {
    4.  
    5.  
    6. QLabel *testme = new QLabel(tr("Please enter the traffic demands associated with each link below test this out:") ) ;
    7.  
    8. QTableWidget *demands = new QTableWidget(this) ;
    9.  
    10. demands->setRowCount(10);
    11. demands->setColumnCount(10);
    12.  
    13. int row = demands->rowCount();
    14. int column = demands->columnCount();
    15. array = new int{MAXPOINTS};
    16.  
    17.  
    18. QTableWidgetItem *flagged = new QTableWidgetItem(" * ") ;
    19. flagged->setFlags(flagged->flags()& ~Qt::ItemIsEditable);
    20.  
    21.  
    22.  
    23. for(int i =0; i <10; i++)
    24. {
    25. for(int j = 0; j<10; j++)
    26. {
    27. demands->setItem(j,i, new QTableWidgetItem("0"));
    28. }
    29. }
    30.  
    31. for(int k = 0; k<10;k++)
    32. {
    33. QTableWidgetItem *flagged = new QTableWidgetItem("*");
    34. flagged->setFlags(flagged->flags()& ~Qt:ItemIsEditable);
    35. demands->setItem(k,k,flagged);
    36. demands->horizontalHeader()->setResizeMode(k,HeaderView:Stretch) ;
    37.  
    38. }
    39.  
    40. /*int multiply = demands->item(2,1)->data(Qt::DisplayRole).toInt();
    41. qDebug("Value of the number is %d", multiply);*/
    42.  
    43. //int item = QVariant(QTableWidgetItem::data(int multiply));
    44. //item->setData(int multiply, int QVariant::toint(bool *ok = 0));
    45. //demands->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
    46.  
    47. qDebug()<<demands->item(2,1)->data(Qt::DisplayRole);
    48.  
    49.  
    50. QTableWidgetItem *flagers = new QTableWidgetItem({"23")};
    51. demands->setItem(2,1,flagers);
    52.  
    53. array[0] = demands->item(2,1)->data(Qt::DisplayRole).toInt();
    54.  
    55.  
    56. QDataStream & operator<<(QDataStream & out, const QTableWidgetItem & flagers);
    57.  
    58.  
    59.  
    60. QVBoxLayout *mainLayout = new QVBoxLayout;
    61. mainLayout->addWidget(testme) ;
    62. mainLayout->addWidget(demands) ;
    63. setLayout(mainLayout);
    64. }
    To copy to clipboard, switch view to plain text mode 

    i have a seperate function where i output the number for outputTable( ) basically its just this:

    display->setText(QString::number(array[0]));
    Last edited by jacek; 26th February 2006 at 22:57. Reason: added code tags

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget

    Quote Originally Posted by therealjag
    display->setText(QString::number(array[0]));
    Where do you change that array[0]? Only in constructor?

    PS. Please, use the code tags to make your code readable.

  12. #12
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    yeah only in the demandsTab function. the array is a global array....i kno i shouldnt do it but its the only way i can seem to get the data over into the other function.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget

    Quote Originally Posted by therealjag
    yeah only in the demandsTab function. the array is a global array....
    But if you change it only in the constructor, it will have always the same value.

    Quote Originally Posted by therealjag
    i kno i shouldnt do it but its the only way i can seem to get the data over into the other function.
    You can use signals instead.

  14. #14
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    could you give me an example please of how a signal is used?

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget


  16. #16
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    ahh ok thanks - ur help is much apreciated

  17. #17
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget

    hey there i was trying to use the cellClicked signal bu the debug mode error message says that cellClicked() SIGNAL doesnt exist. i am also trying to pass in my table but it again says the SLOT doesnt exist??

    my code for the connect is below:
    Qt Code:
    1. connect(demands,SIGNAL(cellClicked(2, 1)),this,SLOT(obtain_values(QTableWidget *demands)));
    To copy to clipboard, switch view to plain text mode 


    the demands table is declared in the demandsTab constructor and the obtain_values function is shown below:

    Qt Code:
    1. void DemandsTab::obtain_values(QTableWidget *demands)
    2. {
    3.  
    4. QTableWidgetItem *flagers = new QTableWidgetItem(("23"));
    5. demands->setItem(1,2,flagers);
    6. array[0] = demands->item(2,1)->data(Qt::DisplayRole).toInt();
    7.  
    8. char test = 'j';
    9. qDebug()<<test;
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    i dont know why it doesnt recognise the table and the signal - any idea's?

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget

    Quote Originally Posted by therealjag
    Qt Code:
    1. connect(demands,SIGNAL(cellClicked(2, 1)),this,SLOT(obtain_values(QTableWidget *demands)));
    To copy to clipboard, switch view to plain text mode 
    1. no parameter values are allowed in SIGNAL and SLOT macros,
    2. no parameter names are allowed in SIGNAL and SLOT macros,
    3. signal and slot signatures must match.


    Try:
    Qt Code:
    1. connect( demands, SIGNAL( cellClicked( int, int ) ), this, SLOT( obtain_values( int, int ) ) );
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. void DemandsTab::obtain_values( int row, int col )
    2. {
    3. // ...
    4. QTableWidget *item = demands->item( row, col );
    5. if( item != 0 ) {
    6. array[0] = ->data(Qt::DisplayRole).toInt();
    7. }
    8. char test = 'j';
    9. qDebug()<<test;
    10. }
    To copy to clipboard, switch view to plain text mode 
    Make sure that obtain_values() is declared as slot and that you have Q_OBJECT macro within class definition.

  19. #19
    Join Date
    Feb 2006
    Posts
    87
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Re: QTableWidget

    i ran the code and it compiled ok but the debugger still says no such signal "cellClicked(int, int)" do i have to put this signal into my header file or something like that, because i tried it before and it still didnt work?

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget

    Could you post the exact error message?

Similar Threads

  1. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2007, 23:29
  2. QTableWidget (resizing rows, turning off selection, etc.)
    By kiss-o-matic in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2007, 01:57
  3. QTableWidget issues
    By Djony in forum Qt Programming
    Replies: 42
    Last Post: 19th December 2006, 23:27
  4. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

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.