Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *.pro file*
- ```
- QT += core gui
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
- TARGET = TestSimpleTableWidget
- TEMPLATE = app
- DEFINES += QT_DEPRECATED_WARNINGS
- CONFIG += c++11
- SOURCES += \
- main.cpp \
- testmainwindow.cpp \
- testtableform.cpp
- HEADERS += \
- testmainwindow.h \
- testtableform.h
- FORMS +=
- # Default rules for deployment.
- qnx: target.path = /tmp/$${TARGET}/bin
- else: unix:!android: target.path = /opt/$${TARGET}/bin
- !isEmpty(target.path): INSTALLS += target
- ```
- *mainwindow.h*
- ```
- #ifndef TESTMAINWINDOW_H
- #define TESTMAINWINDOW_H
- #include <QMainWindow>
- #include <QPushButton>
- #include <QVBoxLayout>
- #include "testtableform.h"
- class TestMainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit TestMainWindow(QWidget *parent = nullptr);
- ~TestMainWindow();
- private:
- TestTableForm * _tableForm;
- QPushButton * _butOpen;
- };
- #endif // TESTMAINWINDOW_H
- ```
- *mainwindow.cpp*
- ```
- #include "testmainwindow.h"
- #include <QDebug>
- TestMainWindow::TestMainWindow(QWidget *parent) : QMainWindow(parent),
- _tableForm(new TestTableForm),
- _butOpen(new QPushButton("Ouvrir test"))
- {
- this->setCentralWidget(_butOpen);
- // on click : show tableWidget
- QObject::connect(_butOpen,&QPushButton::clicked,
- _tableForm, &QWidget::show);
- }
- TestMainWindow::~TestMainWindow()
- {
- delete _tableForm;
- delete _butOpen;
- }
- ```
- *tableform.h*
- ```c++
- #ifndef TESTTABLEFORM_H
- #define TESTTABLEFORM_H
- #include <QWidget>
- #include <QTableWidget>
- #include <QVBoxLayout>
- class TestTableForm : public QWidget
- {
- Q_OBJECT
- public:
- explicit TestTableForm(QWidget *parent = nullptr);
- ~TestTableForm();
- // workaround...
- void closeEvent(QCloseEvent * event);
- private:
- QTableWidget * _table;
- QVBoxLayout * _layout;
- };
- #endif // TESTTABLEFORM_H
- ```
- *tableform.cpp*
- ```c++
- #include <QDebug>
- #include <QCloseEvent>
- TestTableForm::TestTableForm(QWidget *parent) : QWidget(parent),
- _table(new QTableWidget),
- _layout(new QVBoxLayout)
- {
- QStringList head;
- head.append("A"); head.append("A"); head.append("A");
- head.append("A"); head.append("A"); head.append("A");
- head.append("A"); head.append("A"); head.append("A");
- _table->setHorizontalHeaderLabels(head);
- _table->setColumnCount(head.size());
- _table->setRowCount(20);
- _layout->addWidget(_table);
- this->setLayout(_layout);
- }
- TestTableForm::~TestTableForm()
- {
- delete _table;
- }
- void TestTableForm::closeEvent(QCloseEvent *event)
- {
- qDebug() << "Closing... Not ! But Hiding.";
- event->ignore();
- this->hide();
- }
- ```
- *main*
- ```
- #include <QApplication>
- #include "testmainwindow.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- TestMainWindow w;
- w.show();
- return a.exec();
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement