Evgenii Legotckoi
Sept. 4, 2015, 9:30 p.m.

Qt/C++ - Lesson 015. QTableWidget – How to create a table with checkboxes?

Using QTableWidget will be the first in a manner that will advise you to create tables with checkboxes on Qt. So let's look at this option and work with tables in Qt and certainly applicable checkboxes.

So, the lesson to be more close to reality, grabbed some code from lesson QDataWidgetMapper . Namely, take a class to work with the database, so they'd just do a table from the database. After that, make the shape of the main application window and output data from the table with the mapping checkboxes. Naturally, when the application database table is created and populated by several records, which we will display in the widget.


Project Structure for QTableWidget

I suggest to get acquainted with the project structure:

  • QTableWidgetExample.pro - profile;
  • mainwindow.h - header file of the main application window;
  • mainwindow.cpp - source of window;
  • main.cpp - the main source file from which the application starts;
  • mainwindow.ui - form of the main application window;
  • database.h - header file of helper class to be used for information that is placed in a database;
  • database.cpp - source of helper class file to be used for information that is placed in a database;

mainwindow.ui

All you need to do with this file, it is set the QTableWidget in the form of the main window in the designer.

mainwindow.h

This file is declared to the database object with which we work, as well as a method for filling QTableWidget data.

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QSqlQuery>
  6.  
  7. /* My includes */
  8. #ifndef MAINWINDOW_H
  9. #define MAINWINDOW_H
  10.  
  11. #include <QMainWindow>
  12. #include <QSqlQuery>
  13.  
  14. /* My includes */
  15. #include <database.h>
  16.  
  17. namespace Ui {
  18. class MainWindow;
  19. }
  20.  
  21. class MainWindow : public QMainWindow
  22. {
  23. Q_OBJECT
  24.  
  25. public:
  26. explicit MainWindow(QWidget *parent = 0);
  27. ~MainWindow();
  28.  
  29. private:
  30. Ui::MainWindow *ui;
  31. DataBase *db;
  32.  
  33. private:
  34. void createUI(const QStringList &headers);
  35. };
  36.  
  37. #endif // MAINWINDOW_H

mainwindow.cpp

This file is contained in all the purpose of the lesson, namely QTableWidget setting and filling his records from the database.

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9.  
  10. this->setWindowTitle("QTableWidget Example");
  11. /* The first step is to create an object for the database
  12. * and initialize the database connection
  13. * */
  14. db = new DataBase();
  15. db->connectToDataBase();
  16.  
  17. /* Fill the database records */
  18. for(int i = 1; i < 5; i++){
  19. /* Insert a record into the table, immediately set the state of the checkbox.
  20. * If the device has an odd number, the status of the checkbox to true, otherwise false
  21. * */
  22. db->inserIntoDeviceTable(QVariantList() << QString::number(i & 1)
  23. << "Device " + QString::number(i)
  24. << "192.168.0." + QString::number(i)
  25. << "AA:AA:AA:AA:AA:A" + QString::number(i));
  26. }
  27.  
  28. this->createUI(QStringList() << trUtf8("id")
  29. << trUtf8("Нечетность")
  30. << trUtf8("Имя компьютера")
  31. << trUtf8("IP адрес")
  32. << trUtf8("MAC адрес")
  33. );
  34. }
  35.  
  36. MainWindow::~MainWindow()
  37. {
  38. delete ui;
  39. }
  40.  
  41. /* The method to configure the interface,
  42. * the method will be carried out to fill QTableWidget records from the table
  43. * */
  44. void MainWindow::createUI(const QStringList &headers)
  45. {
  46. ui->tableWidget->setColumnCount(5);
  47. ui->tableWidget->setShowGrid(true);
  48. ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
  49. ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
  50. ui->tableWidget->setHorizontalHeaderLabels(headers);
  51. ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
  52. ui->tableWidget->hideColumn(0);
  53.  
  54. QSqlQuery query("SELECT "
  55. DEVICE ".id, "
  56. DEVICE "." DEVICE_CHECK_STATE ", "
  57. DEVICE "." DEVICE_HOSTNAME ", "
  58. DEVICE "." DEVICE_IP ", "
  59. DEVICE "." DEVICE_MAC
  60. " FROM " DEVICE);
  61.  
  62. /* Perform filling QTableWidget records using a loop
  63. * */
  64. for(int i = 0; query.next(); i++){
  65. // Insert row
  66. ui->tableWidget->insertRow(i);
  67. /* Set the id column in the first taking it from the result of the SQL-query.
  68. * This column will be hidden
  69. * */
  70. ui->tableWidget->setItem(i,0, new QTableWidgetItem(query.value(0).toString()));
  71.  
  72. // Create an element, which will serve as a checkbox
  73. QTableWidgetItem *item = new QTableWidgetItem();
  74. item->data(Qt::CheckStateRole);
  75. /* Check on the status of odd if an odd device,
  76. * exhibiting state of the checkbox in the Checked, Unchecked otherwise
  77. * */
  78. if(query.value(1).toInt() == 1){
  79. item->setCheckState(Qt::Checked);
  80. } else {
  81. item->setCheckState(Qt::Unchecked);
  82. }
  83. // Set the checkbox in the second column
  84. ui->tableWidget->setItem(i,1, item);
  85. // Next, pick up all the data from a result set in other fields
  86. ui->tableWidget->setItem(i,2, new QTableWidgetItem(query.value(2).toString()));
  87. ui->tableWidget->setItem(i,3, new QTableWidgetItem(query.value(3).toString()));
  88. ui->tableWidget->setItem(i,4, new QTableWidgetItem(query.value(4).toString()));
  89. }
  90.  
  91. ui->tableWidget->resizeColumnsToContents();
  92. }

database.h

This file is different from what has been taken from the lessons of QDataWidgetMapper that has been added to define a directive for the checkbox, respectively, this resulted in a change in the methods database.cpp file. Namely insertIntoDeviceTable and createDeviceTable .

  1. #ifndef DATABASE_H
  2. #define DATABASE_H
  3.  
  4. #include <QObject>
  5. #include <QSql>
  6. #include <QSqlQuery>
  7. #include <QSqlError>
  8. #include <QSqlDatabase>
  9. #include <QFile>
  10. #include <QDate>
  11. #include <QDebug>
  12.  
  13. #define DATABASE_HOSTNAME "ExampleDataBase"
  14. #define DATABASE_NAME "DataBase.db"
  15.  
  16. #define DEVICE "DeviceTable"
  17. #define DEVICE_CHECK_STATE "CheckState"
  18. #define DEVICE_HOSTNAME "Hostname"
  19. #define DEVICE_IP "IP"
  20. #define DEVICE_MAC "MAC"
  21.  
  22. class DataBase : public QObject
  23. {
  24. Q_OBJECT
  25. public:
  26. explicit DataBase(QObject *parent = 0);
  27. ~DataBase();
  28.  
  29. void connectToDataBase();
  30. bool inserIntoDeviceTable(const QVariantList &data);
  31.  
  32. private:
  33. QSqlDatabase db;
  34.  
  35. private:
  36. bool openDataBase();
  37. bool restoreDataBase();
  38. void closeDataBase();
  39. bool createDeviceTable();
  40. };
  41.  
  42. #endif // DATABASE_H

database.cpp

  1. #include "database.h"
  2.  
  3. DataBase::DataBase(QObject *parent) : QObject(parent)
  4. {
  5.  
  6. }
  7.  
  8. DataBase::~DataBase()
  9. {
  10.  
  11. }
  12.  
  13. void DataBase::connectToDataBase()
  14. {
  15. if(!QFile("C:/example/" DATABASE_NAME).exists()){
  16. this->restoreDataBase();
  17. } else {
  18. this->openDataBase();
  19. }
  20. }
  21.  
  22. bool DataBase::restoreDataBase()
  23. {
  24. if(this->openDataBase()){
  25. if(!this->createDeviceTable()){
  26. return false;
  27. } else {
  28. return true;
  29. }
  30. } else {
  31. qDebug() << "Failed to restore the database";
  32. return false;
  33. }
  34. return false;
  35. }
  36.  
  37. bool DataBase::openDataBase()
  38. {
  39. db = QSqlDatabase::addDatabase("QSQLITE");
  40. db.setHostName(DATABASE_HOSTNAME);
  41. db.setDatabaseName("C:/example/" DATABASE_NAME);
  42. if(db.open()){
  43. return true;
  44. } else {
  45. return false;
  46. }
  47. }
  48.  
  49. void DataBase::closeDataBase()
  50. {
  51. db.close();
  52. }
  53.  
  54. bool DataBase::createDeviceTable()
  55. {
  56. QSqlQuery query;
  57. if(!query.exec( "CREATE TABLE " DEVICE " ("
  58. "id INTEGER PRIMARY KEY AUTOINCREMENT, "
  59. DEVICE_CHECK_STATE " INTEGER NOT NULL,"
  60. DEVICE_HOSTNAME " VARCHAR(255) NOT NULL,"
  61. DEVICE_IP " VARCHAR(16) NOT NULL,"
  62. DEVICE_MAC " VARCHAR(18) NOT NULL"
  63. " )"
  64. )){
  65. qDebug() << "DataBase: error of create " << DEVICE;
  66. qDebug() << query.lastError().text();
  67. return false;
  68. } else {
  69. return true;
  70. }
  71. return false;
  72. }
  73.  
  74. bool DataBase::inserIntoDeviceTable(const QVariantList &data)
  75. {
  76. QSqlQuery query;
  77. query.prepare("INSERT INTO " DEVICE " ( " DEVICE_CHECK_STATE ", "
  78. DEVICE_HOSTNAME ", "
  79. DEVICE_IP ", "
  80. DEVICE_MAC " ) "
  81. "VALUES (:CheckState, :Hostname, :IP, :MAC )");
  82. query.bindValue(":CheckState", data[0].toInt());
  83. query.bindValue(":Hostname", data[1].toString());
  84. query.bindValue(":IP", data[2].toString());
  85. query.bindValue(":MAC", data[3].toString());
  86.  
  87. if(!query.exec()){
  88. qDebug() << "error insert into " << DEVICE;
  89. qDebug() << query.lastError().text();
  90. return false;
  91. } else {
  92. return true;
  93. }
  94. return false;
  95. }

Result

As a result, when you run the program will create an application, which will be a table with four entries, two of which are marked with checkboxes.

Do you like it? Share on social networks!

Terabaytus
  • June 13, 2017, 2:39 p.m.

Спасибо за статью, не подскажите  как можно использовать отмеченные чек боксом строчки. Мне их нужно скопировать их в другой QTableWidgetItem ?

Evgenii Legotckoi
  • June 15, 2017, 11:33 a.m.

Ответил на форуме .

ЮВ
  • Jan. 12, 2019, 10:51 p.m.

В файле mainwindow.cpp строка 86:
item->data(Qt::CheckStateRole); // без слов

Evgenii Legotckoi
  • Jan. 14, 2019, 2:28 p.m.

ну да, затесался неудалённый кусок ерунды, надо будет отредактировать на досуге статью.

Ruslan Polupan
  • April 5, 2019, 5:50 p.m.

Доброго времени суток.
Возник вопрос по статье.
Как организовать сортировку по столбцу с CheckBox.
Т.е. чтобы отмеченные строки отображались в верхней части TableWidget.
Дополнительный скрытый столбец делать или есть еще варианты?

Evgenii Legotckoi
  • April 5, 2019, 7:10 p.m.
  • (edited)

Добрый день. Добавьте ORDER BY в заппрос SELECT по столбцу с чекбоксами.

Ruslan Polupan
  • April 7, 2019, 1:48 a.m.

Дело в том что чтолбец с чекбоксами никак не связан с базой. Он только для выбора объектов.

Evgenii Legotckoi
  • April 8, 2019, 1:27 p.m.

На ум приходит несколько вариантов:

  • добавлять все взятые из БД записи в вектор структур данных, в которых будет дополнительное поле под чекбокс и выполнять сортировку этого вектора по полю чекбокса.
  • Всё тоже самое, только уже переписать на QTableView и сделать полноценную модель данных.

Можно конечно, как-то написать костыли на изменение позиций строчек при наличии выделенного чекбокса, но это будут костыли. Лучше уж перейти на полноценную модель данных в данном случае и QTableView

ВК
  • Sept. 9, 2020, 4:35 p.m.
  • (edited)

Попробовал запустить код, описанный в данной статье, но получаю следующее:

Подскажите в чем может быть проблема ?
Вывод окна - пустой:

Evgenii Legotckoi
  • Sept. 9, 2020, 4:42 p.m.

Пока добавляли у себя код, что-то пробовали проверяли, могло дойти до ситуации, когда у вас получилась создана таблица, с количеством колонок, не совпадающим с количеством колонок в финальной версии. В общем в INSERT запросе у вас больше или меньше аргументов, чем колонок в таблице DEVICE по факту.

Да, у меня тоже такая мысль возникла, но я просто скопипастил этот код из статьи, ничего в нем не меняя.

ВК
  • Sept. 9, 2020, 4:56 p.m.
  • (edited)

Кажется я понял в чем ошибка - я вручную создал таблицу Device в базе данных DataBase.db через DB Browser for SQLite в корне проекта с соответствующими типами данных и по какой-то причине insert не выполнялся, как только БД удалил, пример заработал.

ВК
  • Sept. 29, 2020, 6:08 p.m.

Кто-нибудь знает, как сделать так, чтобы в QTableWidget состоящей из чекбоксов в строке таблицы можно было выбрать только один checkbox ?

СБ
  • Dec. 26, 2020, 5:12 p.m.
  • (edited)

ошибки куда-то пропали, но база данных не заполняется

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html