Results 1 to 6 of 6

Thread: How to add QComboBoxes into QTableWidgets & read and write file data into QComboBox

  1. #1
    Join Date
    Jun 2011
    Posts
    26
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default How to add QComboBoxes into QTableWidgets & read and write file data into QComboBox

    Hello,
    I want to add QComboBoxes into QTableWidgets. i have a table with 13*13.Columns 8,9,10,11 and 12 of each row has a QCombobox.I want to read a file data and write it to this Qtable.Part of read data has to be written into these QComboBoxes.Please help me in achieving this ASAP.

    Thanks in advance..

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to add QComboBoxes into QTableWidgets & read and write file data into QComboB

    Create a QComboBox, fill the data data from file into the combo box and then set it on to the required cell on the QTableWidget, like this

    Qt Code:
    1. QComboBox* combo = new QComboBox();
    2. //add some data to combo
    3.  
    4. tableWidget->setCellWidget(row, column, combo); //table widget takes the owership so need not delete combobox explicitly.
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2011
    Posts
    26
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add QComboBoxes into QTableWidgets & read and write file data into QComboB

    what is floor1Box? where is it defined?

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to add QComboBoxes into QTableWidgets & read and write file data into QComboB

    Quote Originally Posted by Channareddy
    table->setCellWidget(r,8,floor1Box) //the comboBox is not being displayed in Qtable but it is displayed when i write it this way
    You need make sure that a QTableWidgetItem is already present (already added) in the cell. More over y I guess ou should be adding "floor1ComboBox" not "floor1Box". What is "floor1Box"? Is it a typo error?

    Quote Originally Posted by Channareddy
    table->setCellWidget(r,8,new QComboBox(floor1Box)//here combo Box is displayed without data.
    You are creating a second QComboBox, not required.

    Quote Originally Posted by Channareddy
    I think data addition into QComboBox is done in wrong way by me.
    Please tell me how to add strings or stingList into Combo Box and that displaying problem.
    You are doing it right, both string and stringlist will work. Problem is with inserting into the cell, as I said make sure item is already present in the cell.


    Added after 16 minutes:


    Here is working example, read the inline comments, see what you have missed to do

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. QWidget widget;
    8. widget.setLayout(new QGridLayout(&widget));
    9.  
    10. // Create a TableWidget
    11. QTableWidget* tableWidget = new QTableWidget(&widget);
    12.  
    13. // Add Table Widget to main window
    14. widget.layout()->addWidget(tableWidget);
    15.  
    16. // Make TableWidget to 2 Rows x 2 Columns
    17. tableWidget->setColumnCount(2); // This is required
    18. tableWidget->setRowCount(2); // Do this or call tableWidget->insertRow() later
    19.  
    20. // Add 4 Items to the TableWidget
    21. tableWidget->setItem(0, 0, new QTableWidgetItem("Row 1, Col: 1"));
    22. tableWidget->setItem(0, 1, new QTableWidgetItem("Row 1, Col: 2"));
    23. tableWidget->setItem(1, 0, new QTableWidgetItem("Row 1, Col: 1"));
    24. tableWidget->setItem(1, 1, new QTableWidgetItem("Row 2, Col: 2"));
    25.  
    26. // Prepare ComboBox
    27. QComboBox *comboBox = new QComboBox(&widget);
    28. comboBox->addItems(QStringList() << "Option 1" << "Option 2" << "Option 3" << "Option 4");
    29.  
    30. // Add ComboBox at 1st row 1st column of TableWidget
    31. tableWidget->setCellWidget(0, 0, comboBox);
    32.  
    33. // Show main window
    34. widget.show();
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

    This how it looks
    TableCombo.png
    Last edited by Santosh Reddy; 10th June 2011 at 09:18.

  6. #6
    Join Date
    Jun 2011
    Posts
    26
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: How to add QComboBoxes into QTableWidgets & read and write file data into QComboB

    Thank you.. With your help i got it..

Similar Threads

  1. read and write data in excel files
    By robotics in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2011, 01:13
  2. Read/write data from file
    By Insomnium in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2010, 08:35
  3. Replies: 2
    Last Post: 2nd November 2010, 06:15
  4. How to read and write data into the QTableView
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2009, 13:31
  5. file read & write help....
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 17th November 2008, 12:58

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.