PDA

View Full Version : How to add QComboBoxes into QTableWidgets & read and write file data into QComboBox



Channareddy
9th June 2011, 15:11
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..:)

Santosh Reddy
9th June 2011, 15:24
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


QComboBox* combo = new QComboBox();
//add some data to combo

tableWidget->setCellWidget(row, column, combo); //table widget takes the owership so need not delete combobox explicitly.

Channareddy
10th June 2011, 05:31
Hi..good morning..:)

I added QComboBox into QTableWidget but the data is not being added into QComboBox.Also there is problem in QComboBox display.
Here is my code e.g
[code]

QString boxString("Door1");
QComboBox *floor1ComboBox = new QComboBox(this);
floor1ComboBox->addItem(boxString);//this item is not being added to the comboBox

//I also tried using QStringList in place of QString as below but this is also not working
floor1ComboBox->addItems(boxStringList);

//Also while adding QComboBox into QTable has a problem

table->setCellWidget(r,8,floor1Box) //the comboBox is not being displayed in Qtable but it is displayed when i write it this way

table->setCellWidget(r,8,new QComboBox(floor1Box)//here combo Box is displayed without data.


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.

FelixB
10th June 2011, 07:45
what is floor1Box? where is it defined?

Santosh Reddy
10th June 2011, 08:18
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?


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


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


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QWidget widget;
widget.setLayout(new QGridLayout(&widget));

// Create a TableWidget
QTableWidget* tableWidget = new QTableWidget(&widget);

// Add Table Widget to main window
widget.layout()->addWidget(tableWidget);

// Make TableWidget to 2 Rows x 2 Columns
tableWidget->setColumnCount(2); // This is required
tableWidget->setRowCount(2); // Do this or call tableWidget->insertRow() later

// Add 4 Items to the TableWidget
tableWidget->setItem(0, 0, new QTableWidgetItem("Row 1, Col: 1"));
tableWidget->setItem(0, 1, new QTableWidgetItem("Row 1, Col: 2"));
tableWidget->setItem(1, 0, new QTableWidgetItem("Row 1, Col: 1"));
tableWidget->setItem(1, 1, new QTableWidgetItem("Row 2, Col: 2"));

// Prepare ComboBox
QComboBox *comboBox = new QComboBox(&widget);
comboBox->addItems(QStringList() << "Option 1" << "Option 2" << "Option 3" << "Option 4");

// Add ComboBox at 1st row 1st column of TableWidget
tableWidget->setCellWidget(0, 0, comboBox);

// Show main window
widget.show();
return a.exec();
}

This how it looks
6569

Channareddy
10th June 2011, 10:58
Thank you..:) With your help i got it..