c++ - Qt - Adding data to a TableWidget via another TableWidget -
i stuck on problem table widgets , in need of help. basically, trying table widget on dialogue screen information inside of , fill information on table widget on main window screen. so, when click ok button, should take text 1 table widget , place in other table widget. tried using below code, , build ran. but, clicked ok, program crashed. table called tablewidgetedit table trying copy , send table in mainwindow, named tablewidget. (just make not vague, trying copy data 1 table , place in table, when user clicks on ok button.)
int rows = 6; int columns = 5; ui::mainwindow *mainui; void editmode1::on_pushbutton_clicked() { (int = 0; i<columns;++i){ (int j = 0;j<rows;++j){ qtablewidgetitem *celltxt= tablewidgetedit.item(j,i); mainui->tablewidget->setitem(j,i,celltxt); } } } any , appreciated. thank you! (i not best qt if don't mind explaining changes have made , why, great thank you!).
-update- @jeet here code im trying do:
tablemainwindow1.h:
#ifndef tablemainwindow1_h #define tablemainwindow1_h #include <qmainwindow> #include "tabledialougewindow.h" namespace ui { class tablemainwindow1; } class tablemainwindow1 : public qmainwindow { q_object public: explicit tablemainwindow1(qwidget *parent = 0); ~tablemainwindow1(); private slots: void on_pushbutton_clicked(); private: ui::tablemainwindow1 *ui; tabledialougewindow *tbl2; }; #endif // tablemainwindow1_h tablemainwindow1.cpp:
#include "tablemainwindow1.h" #include "ui_tablemainwindow1.h" tablemainwindow1::tablemainwindow1(qwidget *parent) : qmainwindow(parent), ui(new ui::tablemainwindow1) { ui->setupui(this); ui->tablewidget->setrowcount(3); ui->tablewidget->setcolumncount(3); } tablemainwindow1::~tablemainwindow1() { delete ui; } void tablemainwindow1::on_pushbutton_clicked() { tbl2 = new tabledialougewindow(this); tbl2->show(); } tabledialougewindow.h:
#ifndef tabledialougewindow_h #define tabledialougewindow_h #include <qdialog> namespace ui { class tabledialougewindow; } class tabledialougewindow : public qdialog { q_object public: explicit tabledialougewindow(qwidget *parent = 0); ~tabledialougewindow(); private slots: void on_buttonbox_accepted(); private: ui::tabledialougewindow *ui; }; #endif // tabledialougewindow_h tabledialougewindow.cpp:
#include "tabledialougewindow.h" #include "ui_tabledialougewindow.h" #include "tablemainwindow1.h" #include "ui_tablemainwindow1.h" int rows = 3; int columns = 3; ui::tablemainwindow1 *mainui; tabledialougewindow::tabledialougewindow(qwidget *parent) : qdialog(parent), ui(new ui::tabledialougewindow) { ui->setupui(this); ui->tablewidget->setrowcount(rows); ui->tablewidget->setcolumncount(columns); } tabledialougewindow::~tabledialougewindow() { delete ui; } void tabledialougewindow::on_buttonbox_accepted() { for(int = 0;i<columns;++i){ for(int j = 0;j<rows;++j){ qtablewidgetitem *celltxt = ui->tablewidget->item(j,i); qtablewidgetitem *celltxt2 =new qtablewidgetitem(*celltxt); mainui->tablewidget->setitem(j,i,celltxt2); } } accept(); } hope helps.
the error cannot insert item owned qtablewidget. before make copy need clone data. can use copy constructor achieve this. code comment explain in detail. hope help.
void mainwindow::on_cmdtransfer_clicked() { //source table int rows = ui->tbl1->rowcount(); int columns =ui->tbl1->columncount(); //destination table ui->tbl2->setcolumncount(columns); ui->tbl2->setrowcount(rows); //copy data form 1 table another. (int = 0; i<columns;++i){ (int j = 0;j<rows;++j){ qtablewidgetitem *celltxt= ui->tbl1->item(j,i); //clone data using copy constructor qtablewidgetitem *celltxt1=new qtablewidgetitem(*celltxt); ui->tbl2->setitem(j,i,celltxt1); } } } 
Comments
Post a Comment