Advertisement
Guest User

qtquestionplou

a guest
Jul 9th, 2019
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. *.pro file*
  2. ```
  3. QT += core gui
  4.  
  5. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  6.  
  7. TARGET = TestSimpleTableWidget
  8. TEMPLATE = app
  9. DEFINES += QT_DEPRECATED_WARNINGS
  10.  
  11. CONFIG += c++11
  12.  
  13. SOURCES += \
  14. main.cpp \
  15. testmainwindow.cpp \
  16. testtableform.cpp
  17.  
  18. HEADERS += \
  19. testmainwindow.h \
  20. testtableform.h
  21.  
  22. FORMS +=
  23.  
  24. # Default rules for deployment.
  25. qnx: target.path = /tmp/$${TARGET}/bin
  26. else: unix:!android: target.path = /opt/$${TARGET}/bin
  27. !isEmpty(target.path): INSTALLS += target
  28. ```
  29.  
  30. *mainwindow.h*
  31. ```
  32. #ifndef TESTMAINWINDOW_H
  33. #define TESTMAINWINDOW_H
  34.  
  35. #include <QMainWindow>
  36. #include <QPushButton>
  37. #include <QVBoxLayout>
  38.  
  39. #include "testtableform.h"
  40.  
  41. class TestMainWindow : public QMainWindow
  42. {
  43. Q_OBJECT
  44. public:
  45. explicit TestMainWindow(QWidget *parent = nullptr);
  46. ~TestMainWindow();
  47. private:
  48. TestTableForm * _tableForm;
  49. QPushButton * _butOpen;
  50. };
  51.  
  52. #endif // TESTMAINWINDOW_H
  53. ```
  54. *mainwindow.cpp*
  55. ```
  56. #include "testmainwindow.h"
  57. #include <QDebug>
  58.  
  59. TestMainWindow::TestMainWindow(QWidget *parent) : QMainWindow(parent),
  60. _tableForm(new TestTableForm),
  61. _butOpen(new QPushButton("Ouvrir test"))
  62. {
  63. this->setCentralWidget(_butOpen);
  64. // on click : show tableWidget
  65. QObject::connect(_butOpen,&QPushButton::clicked,
  66. _tableForm, &QWidget::show);
  67. }
  68.  
  69. TestMainWindow::~TestMainWindow()
  70. {
  71. delete _tableForm;
  72. delete _butOpen;
  73. }
  74. ```
  75. *tableform.h*
  76. ```c++
  77. #ifndef TESTTABLEFORM_H
  78. #define TESTTABLEFORM_H
  79.  
  80. #include <QWidget>
  81. #include <QTableWidget>
  82. #include <QVBoxLayout>
  83.  
  84. class TestTableForm : public QWidget
  85. {
  86. Q_OBJECT
  87. public:
  88. explicit TestTableForm(QWidget *parent = nullptr);
  89. ~TestTableForm();
  90. // workaround...
  91. void closeEvent(QCloseEvent * event);
  92.  
  93. private:
  94. QTableWidget * _table;
  95. QVBoxLayout * _layout;
  96. };
  97.  
  98. #endif // TESTTABLEFORM_H
  99. ```
  100. *tableform.cpp*
  101. ```c++
  102. #include <QDebug>
  103. #include <QCloseEvent>
  104.  
  105. TestTableForm::TestTableForm(QWidget *parent) : QWidget(parent),
  106. _table(new QTableWidget),
  107. _layout(new QVBoxLayout)
  108. {
  109. QStringList head;
  110. head.append("A"); head.append("A"); head.append("A");
  111. head.append("A"); head.append("A"); head.append("A");
  112. head.append("A"); head.append("A"); head.append("A");
  113. _table->setHorizontalHeaderLabels(head);
  114. _table->setColumnCount(head.size());
  115. _table->setRowCount(20);
  116. _layout->addWidget(_table);
  117.  
  118. this->setLayout(_layout);
  119. }
  120.  
  121. TestTableForm::~TestTableForm()
  122. {
  123. delete _table;
  124. }
  125.  
  126. void TestTableForm::closeEvent(QCloseEvent *event)
  127. {
  128. qDebug() << "Closing... Not ! But Hiding.";
  129. event->ignore();
  130. this->hide();
  131. }
  132. ```
  133. *main*
  134. ```
  135. #include <QApplication>
  136. #include "testmainwindow.h"
  137.  
  138. int main(int argc, char *argv[])
  139. {
  140. QApplication a(argc, argv);
  141. TestMainWindow w;
  142. w.show();
  143. return a.exec();
  144. }
  145. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement