B
B
BakInRR2015-10-26 18:44:16
MySQL
BakInRR, 2015-10-26 18:44:16

How to handle dynamic cell change of QTableWidgetItem?

How to handle dynamic cell change of QTableWidgetItem? It is necessary to make sure that when a cell is changed, the data in the database is updated.
Thank you in advance.
1320561cb2df4767bac5de4e5ed97cbb.pngb8f7b114d4fb441ca59fbe686e668094.png
MAINWINDOW.H

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H</blockquote>

MAIN.CPP
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

MAINWINDOW.CPP

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QHeaderView>
#include <QTableView>
#include <QCoreApplication>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlDriver>
#include <QDebug>
#include <QSqlQuery>
#include <QTableWidgetItem>
#include <QTableWidget>
#include <QTabWidget>
#include <qtableview.h>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow){
    ui->setupUi(this);
     //ui->tw->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
    //Подключение к серверу БД и авторизация
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("**");
    db.setDatabaseName("db5");
    db.setUserName("goodjob");
    db.setPassword("3214324");
    //Если подключиться удалось...

    if (db.open())
    {

        for (int i=0; i<2; i++)
        {
            ui->tw->insertColumn(i);
            switch (i) {
            case 0:
            {
                ui->tw->setHorizontalHeaderItem(i, new QTableWidgetItem("id"));
                break;
            }
            case 1:
            {
                ui->tw->setHorizontalHeaderItem(i, new QTableWidgetItem("node_type_name"));
                break;
            }



            }


        }
        QSqlQuery q;
        q.exec("select * from node_types");
        //q.first();
        while (q.next())
        {
            ui->tw->insertRow(ui->tw->rowCount());
            for (int i=0; i<2; i++)
            {
                QTableWidgetItem * item = new QTableWidgetItem(q.value(i).toString());
                ui->tw->setItem(ui->tw->rowCount()-1,i,item);
            }
        }



        for (int i=0; i<7; i++)
        {
            ui->tw_2->insertColumn(i);
        }
        QSqlQuery q2;
        q2.exec("select * from nodes");
        //q.first();
        while (q2.next())
        {
            ui->tw_2->insertRow(ui->tw_2->rowCount());
            for (int i=0; i<7; i++)
            {
                QTableWidgetItem * item = new QTableWidgetItem(q2.value(i).toString());
                ui->tw_2->setItem(ui->tw_2->rowCount()-1,i,item);
            }
        }




        for (int i=0; i<5; i++)
        {
            ui->tw_3->insertColumn(i);
        }
        QSqlQuery q3;
        q3.exec("select * from edge_types");
        //q.first();
        while (q3.next())
        {
            ui->tw_3->insertRow(ui->tw_3->rowCount());
            for (int i=0; i<5; i++)
            {
                QTableWidgetItem * item = new QTableWidgetItem(q3.value(i).toString());
                ui->tw_3->setItem(ui->tw_3->rowCount()-1,i,item);
            }
        }



        for (int i=0; i<4; i++)
        {
            ui->tw_4->insertColumn(i);


        }
        QSqlQuery q4;
        q4.exec("select * from edges");
        //q.first();
        while (q4.next())
        {
            ui->tw_4->insertRow(ui->tw_4->rowCount());
            for (int i=0; i<4; i++)
            {
                QTableWidgetItem * item = new QTableWidgetItem(q4.value(i).toString());
                ui->tw_4->setItem(ui->tw_4->rowCount()-1,i,item);
            }
        }



       ui->tw->horizontalHeader()->resizeSections(QHeaderView::ResizeToContents);

    }
    else //Если нет - выводим сообщение об ошибке
    {
        ui->tw->insertRow(0); ui->tw->insertColumn(0);
        QTableWidgetItem * item = new QTableWidgetItem("Error: "+db.lastError().text());
        ui->tw->setItem(0,0,item);
    }
}


void QTableWidget::itemChanged ( QTableWidgetItem * item )
{
    QSqlQuery query;
    query.exec("INSERT INTO node_types (node_type_name) VALUES ('Thad Beaucdavxmont')");

}


MainWindow::~MainWindow()
{
    delete ui;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AtomKrieg, 2015-10-26
@BakInRR

Via
QTableWidget
Signals
void cellChanged(int row, int column)

B
BakInRR, 2015-10-27
@BakInRR

Could you be more specific?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question