Results 1 to 11 of 11

Thread: add text to qtablewidget

  1. #1
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question add text to qtablewidget

    hey all,

    i have application with QTableWidget of 3X4. i need to load image along with corresponding name of the icon.

    i have the following code to display text

    Qt Code:
    1. QFont fnt;
    2. fnt.setPointSize(30);
    3. fnt.setFamily("Arial");
    4.  
    5. for (int r = 0; r < t.rowCount(); ++r)
    6. {
    7. for (int c = 0; c < t.columnCount(); ++c)
    8. {
    9. item->setBackgroundColor(Qt::black);
    10. item->setIcon(QIcon(QString(":/images/img%1.png").arg(r * t.columnCount() + c)));
    11.  
    12. item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
    13.  
    14. // writting icon name
    15.  
    16. item->setTextColor(Qt::white);
    17. item->setFont(fnt);
    18. item->setText("help");
    19. item->setTextAlignment(Qt::AlignBottom|Qt::AlignLeading);
    20.  
    21. t.setItem(r, c, item);
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    following code displays help in all the cell. now my question is how would i display different text in each cell based on icon i load.

    hope i am clear.

  2. #2
    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: add text to qtablewidget

    Pass different strings to QTableWidgetItem::setText() instead of passing "help" each time.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: add text to qtablewidget

    i hope you did not get what i meant.
    i will explain i want to display help in cell1 ,file in cell2 , save in cell3 and exit in cell4 (this completes one row) .similarly i want to display different text in 2 more rows.

    hope i am clear now.

  4. #4
    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: add text to qtablewidget

    So what's the problem? What is keeping you from doing that?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: add text to qtablewidget

    it would be helpful if you tel me your suggestion clearly. i really cant understand what are you trying to say.

    item->setText("help");

    since this is inside for loop it prints help in all the cells of 3X4 table. but i want different text in each cell how do i do it.

  6. #6
    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: add text to qtablewidget

    So why did you place it inside that loop in the first place?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: add text to qtablewidget

    i can place that only inside loop if i place it outside i get error as it is not in scope.

    i hope you give me some solution for this.

  8. #8
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: add text to qtablewidget

    hi,

    if u want autonumber u can use the following code,
    Qt Code:
    1. QFont fnt;
    2. fnt.setPointSize(30);
    3. fnt.setFamily("Arial");
    4.  
    5. for (int r = 0; r < t.rowCount(); ++r)
    6. {
    7. for (int c = 0; c < t.columnCount(); ++c)
    8. {
    9. item->setBackgroundColor(Qt::black);
    10. item->setIcon(QIcon(QString(":/images/img%1.png").arg(r * t.columnCount() + c)));
    11.  
    12. item->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
    13.  
    14. // writting icon name
    15.  
    16. item->setTextColor(Qt::white);
    17. item->setFont(fnt);
    18.  
    19. item->setText(QString::number(r *c));
    20. item->setTextAlignment(Qt::AlignBottom|Qt::AlignLeading);
    21.  
    22. t.setItem(r, c, item);
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    if u want to give a specific name to each item.
    then u can use a stringlist to store the name.
    and inside the loop u can use that in setText.
    ex : setText(myImageName.at(i)); something like tat.

    hope it helps
    Bala

  9. #9
    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: add text to qtablewidget

    Quote Originally Posted by rashmi View Post
    i can place that only inside loop if i place it outside i get error as it is not in scope.
    So ask the table widget for the item again outside the inner loop and set the text there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: add text to qtablewidget

    hey bala,

    thanks a lot for your help.

    regards
    rashmi

  11. #11
    Join Date
    Nov 2010
    Posts
    29
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: add text to qtablewidget

    hey bala,

    it is not fixed.it may change...........

Similar Threads

  1. Replies: 1
    Last Post: 18th November 2009, 07:38
  2. Change QTableWidget header name or text
    By ricardo in forum Qt Programming
    Replies: 3
    Last Post: 14th May 2009, 11:37
  3. Layout long text in QTableWidget
    By Tamtam in forum Newbie
    Replies: 0
    Last Post: 13th February 2009, 15:09
  4. Replies: 1
    Last Post: 3rd September 2008, 14:16
  5. QTableWidget setSpan text display problem
    By sureshbabu in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2008, 16:14

Tags for this Thread

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.