Results 1 to 4 of 4

Thread: QTable of comoboxes

  1. #1
    Join Date
    May 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Default QTable of comoboxes

    Hi, i have a QTableWidget of comboboxes.
    I set the comboboxes like this:

    QComboBox *item = new QComboBox();
    ui->tableWidget->setCellWidget(i,0,item);

    my problem is getting information from the combobox later. i need to get a hold of currentText()..
    ui->tableWidget->cellWidget(row,kol)->(no option of currentText())
    how can i get a hold of the combobox?

    thx,

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

    Default Re: QTable of comoboxes

    Quote Originally Posted by terhje View Post
    ui->tableWidget->cellWidget(row,kol)->(no option of currentText())
    I doesn't have an option because that method returns a QWidget* (and QWidget doesn't know anything about the derived class object that it points to)
    You need to cast the pointer, use dynamic_cast or qobject_cast and don't forget to check for 0 (null pointer) especially if you have many kinds of widgets in the table (not only QComboBox)

  3. #3
    Join Date
    May 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3

    Default Re: QTable of comoboxes

    Thx for quick reply! Im a bit of a newbie with this. do you have an example of casting the pointer with dynamic_cast or qobject_cast?
    Thx,

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

    Default Re: QTable of comoboxes

    You have an qobject_cast example in the link i gave you, and it's similar with dynamic_cast (but it is supposed to be used with instances of classes derived from QObject)
    Qt Code:
    1. QComboBox* comboFromTableWidget = dynamic_cast<QComboBox*>(ui->tableWidget->cellWidget(row,kol));
    2. if(comboFromTableWidget != 0) /*test for null pointer (dynamic_cast returns null if the pointer can't be "casted" to a pointer to a derived class)*/
    3. {
    4. //use the combo here... because the pointer holds an address of a QComboBox...
    5. QString text = comboFromTableWidget->currentText();
    6. }
    To copy to clipboard, switch view to plain text mode 

    //i suggest you read a C++ book (the two volumes of Thinking in C++ can be found free of charge - just google it), else you will have a lot of problems - and Qt is actually "cute" and the saying that Qt actually brings to front the "much smaller and cleaner language" that struggle to get out from C++ is true , but still you need pretty good C++ knowledge to enjoy using Qt framework - especially OOP <dynamic_cast is somehow OOP related >

  5. The following user says thank you to Zlatomir for this useful post:

    terhje (12th June 2012)

Similar Threads

  1. QTable Widget
    By kavinsiva in forum Newbie
    Replies: 5
    Last Post: 23rd October 2009, 14:50
  2. QTable Sorting
    By mromey in forum Qt Programming
    Replies: 1
    Last Post: 23rd April 2009, 08:28
  3. Searching a QTable
    By nategoofs in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 23:15
  4. QTable>>>paintCell
    By :db:sStrong in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2006, 11:45
  5. QTable..Vs.. QListView
    By :db:sStrong in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2006, 22:03

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
  •  
Qt is a trademark of The Qt Company.