Unsolved Displaying TableWidgetItem
-
Hi,
I stored the location of item in QTableWidgetItem but when im trying to display the item on another table widget nothing will show.
What im doing wrong?
Here is the code:void MainWindow::on_pushButton_2_clicked() { int row1 = 1; while(row1 <= rowfirstlastname){ int clm1= 1; while(clm1 <= 7){ QTableWidgetItem *table3 = table->item(row1 , clm1 ); table3 = new QTableWidgetItem; ui->tableWidget->setItem(row1,clm1,table3); clm1++; } row1++; }
Thanks
-
Hi,
You are creating a new item and setting that one on
ui->tableWidget
.By the way, if you want to move the item around you should use takeItem.
-
Thanks for your replay
by the way i have used takeitem but didnt worked.
void MainWindow::on_pushButton_2_clicked() { int row1 = 1; while(row1 <= rowfirstlastname){ int clm1= 1; while(clm1 <= 7){ QTableWidgetItem *table3 = table->takeItem(row1 , clm1 ); table3 = new QTableWidgetItem; ui->tableWidget->setItem(row1,clm1,table3); ui->label_3->setText(table3->text()); clm1++; } row1++; } } ``
there is one more question.
im putting strings on another table with this code :QString model = ui->textEdit_2->toPlainText(); QTableWidgetItem *itemobj3 = new QTableWidgetItem; itemobj3->setText(model); table->setItem(rowfirstlastname , 3 , itemobj3 );`
is there any wrong with this too?
Thanks.
-
Because you are still doing the same wrong thing:
QTableWidgetItem *table3 = table->takeItem(row1 , clm1 ); table3 = new QTableWidgetItem; << You are replacing the item you took with a new empty one.
which means you are also leaking memory.
-
i have removed that line but program will crash.
Thanks. -
When does it crash ?
What does the backtrace tell you ?
Are you sure all the constants you use are valid ? -
@SGaist when i click on push button 2 it will crash
its better to put my whole code :.h
class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); int row=1; int clm=7; int rowfirstlastname = 1; QTableWidget *table = new QTableWidget (row,clm); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); private: Ui::MainWindow *ui; };
.cpp
void MainWindow::on_pushButton_clicked() { QString name = ui->textEdit->toPlainText(); QTableWidgetItem *itemobj1 = new QTableWidgetItem; itemobj1->setText(name); ui->tableWidget->setItem(rowfirstlastname , 1 , itemobj1 ); QString make = ui->comboBox_2->currentText(); QTableWidgetItem *itemobj2 = new QTableWidgetItem; itemobj2->setText(make); ui->tableWidget->setItem(rowfirstlastname , 2 , itemobj2 ); QString model = ui->textEdit_2->toPlainText(); QTableWidgetItem *itemobj3 = new QTableWidgetItem; itemobj3->setText(model); ui->tableWidget->setItem(rowfirstlastname , 3 , itemobj3 ); QString year = ui->comboBox_3->currentText(); QTableWidgetItem *itemobj4 = new QTableWidgetItem; itemobj4->setText(year); ui->tableWidget->setItem(rowfirstlastname , 4 , itemobj4 ); QString type = ui->comboBox->currentText(); QTableWidgetItem *itemobj5 = new QTableWidgetItem; itemobj5->setText(type); ui->tableWidget->setItem(rowfirstlastname , 5 , itemobj5 ); QString color = ui->comboBox_3->currentText(); QTableWidgetItem *itemobj6 = new QTableWidgetItem; itemobj6->setText(color); ui->tableWidget->setItem(rowfirstlastname , 6 , itemobj6 ); QString tag = ui->textEdit_3->toPlainText(); QTableWidgetItem *itemobj7 = new QTableWidgetItem; itemobj7->setText(tag); ui->tableWidget->setItem(rowfirstlastname , 7 , itemobj7 ); rowfirstlastname++; } void MainWindow::on_pushButton_2_clicked() { int row1 = 1; while(row1 <= rowfirstlastname){ int clm1= 1; while(clm1 <= 7){ QTableWidgetItem *table3 = ui->tableWidget->takeItem(row1 , clm1 ); ui->tableWidget->setItem(row1,clm1,table3); table3 = new QTableWidgetItem; ui->label_3->setText(table3->text()); clm1++; } row1++; } }
-
From what I gather you have seven columns in your table. In C++, everything that has an index is zero based i.e. a seven element table is accessed from 0 to 6. Therefore your
while(clm1 <= 7)
loop is wrong.Second thing:
QTableWidgetItem *table3 = ui->tableWidget->takeItem(row1 , clm1 ); ui->tableWidget->setItem(row1,clm1,table3); << Why are you taking the item out of tableWidget to put it back at the exact same place ?
-
@SGaist So how i must display the item that i stored in table
thanks -
Use the correct indexes and also in your while loop you overwrite the content of label_3 every time so at best you'll see the content of the last column of the last row.