Newbie in QT: what is wrong with my first QT code?
-
I am new in QT Cpp with Java Form background. My question is can we put all code from file Dialog.cpp and Dialog.h and main.cpp into one file? I want to create a code which is one copy-paste file and F5 to run.
Here is the code, what is wrong with it?#include "mainwindow.h" #include <QAction> #include <QApplication> #include <QComboBox> #include <QDialog> #include <QDialogButtonBox> #include <QFormLayout> #include <QGroupBox> #include <QLabel> #include <QLineEdit> #include <QMainWindow> #include <QMenu> #include <QMenuBar> #include <QPushButton> #include <QSpinBox> #include <QTextEdit> #include <QVBoxLayout> #include <QWidget> /* QT_BEGIN_NAMESPACE class QAction; class QDialogButtonBox; class QGroupBox; class QLabel; class QLineEdit; class QMenu; class QMenuBar; class QPushButton; class QTextEdit; QT_END_NAMESPACE */ //! [0] class Dialog : public QDialog { Q_OBJECT public: Dialog(); private: void createMenu(); void createHorizontalGroupBox(); void createGridGroupBox(); void createFormGroupBox(); enum { NumGridRows = 3, NumButtons = 4 }; QMenuBar *menuBar; QGroupBox *horizontalGroupBox; QGroupBox *gridGroupBox; QGroupBox *formGroupBox; QTextEdit *smallEditor; QTextEdit *bigEditor; QLabel *labels[NumGridRows]; QLineEdit *lineEdits[NumGridRows]; QPushButton *buttons[NumButtons]; QDialogButtonBox *buttonBox; QMenu *fileMenu; QAction *exitAction; }; //! [0] Dialog::Dialog() { createMenu(); createHorizontalGroupBox(); createGridGroupBox(); createFormGroupBox(); bigEditor = new QTextEdit; bigEditor->setPlainText( tr( "This widget takes up all the remaining space " "in the top-level layout." ) ); buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel ); connect( buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept ); connect( buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject ); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->setMenuBar( menuBar ); mainLayout->addWidget( horizontalGroupBox ); mainLayout->addWidget( gridGroupBox ); mainLayout->addWidget( formGroupBox ); mainLayout->addWidget( bigEditor ); mainLayout->addWidget( buttonBox ); setLayout( mainLayout ); setWindowTitle( tr( "Basic Layouts" ) ); } //! [6] void Dialog::createMenu() { menuBar = new QMenuBar; fileMenu = new QMenu( tr( "&File" ), this ); exitAction = fileMenu->addAction( tr( "E&xit" ) ); menuBar->addMenu( fileMenu ); connect( exitAction, &QAction::triggered, this, &QDialog::accept ); } //! [6] //! [7] void Dialog::createHorizontalGroupBox() { horizontalGroupBox = new QGroupBox( tr( "Horizontal layout" ) ); QHBoxLayout *layout = new QHBoxLayout; for ( int i = 0; i < NumButtons; ++i ) { buttons[i] = new QPushButton( tr( "Button %1" ).arg( i + 1 ) ); layout->addWidget( buttons[i] ); } horizontalGroupBox->setLayout( layout ); } //! [7] //! [8] void Dialog::createGridGroupBox() { gridGroupBox = new QGroupBox( tr( "Grid layout" ) ); //! [8] QGridLayout *layout = new QGridLayout; //! [9] for ( int i = 0; i < NumGridRows; ++i ) { labels[i] = new QLabel( tr( "Line %1:" ).arg( i + 1 ) ); lineEdits[i] = new QLineEdit; layout->addWidget( labels[i], i + 1, 0 ); layout->addWidget( lineEdits[i], i + 1, 1 ); } //! [9] //! [10] smallEditor = new QTextEdit; smallEditor->setPlainText( tr( "This widget takes up about two thirds of the " "grid layout." ) ); layout->addWidget( smallEditor, 0, 2, 4, 1 ); //! [10] //! [11] layout->setColumnStretch( 1, 10 ); layout->setColumnStretch( 2, 20 ); gridGroupBox->setLayout( layout ); } //! [11] //! [12] void Dialog::createFormGroupBox() { formGroupBox = new QGroupBox( tr( "Form layout" ) ); QFormLayout *layout = new QFormLayout; layout->addRow( new QLabel( tr( "Line 1:" ) ), new QLineEdit ); layout->addRow( new QLabel( tr( "Line 2, long text:" ) ), new QComboBox ); layout->addRow( new QLabel( tr( "Line 3:" ) ), new QSpinBox ); formGroupBox->setLayout( layout ); } int main( int argc, char *argv[] ) { QApplication app( argc, argv ); Dialog dialog; #ifdef Q_OS_ANDROID dialog.showMaximized(); #else dialog.show(); #endif return app.exec(); }
Here is the errors:
error: undefined reference tovtable for Dialog' error: undefined reference to
Dialog::staticMetaObject'
:-1: error: collect2.exe: error: ld returned 1 exit status
:-1: error: [Makefile.Debug:72: debug/QTProject001.exe] Error 1Here is the reference link:
https://code.qt.io/cgit/qt/qtbase.git/tree/examples/widgets/layouts/basiclayouts -
Hi, and welcome!
@ElfMoon said in Newbie in QT: what is wrong with my first QT code?:
can we put all code from file Dialog.cpp and Dialog.h and main.cpp into one file?
It is possible to do this. However, you must then write
#include "moc_XXX.cpp"
in your file (whereXXX.cpp
is the name of your file). See https://forum.qt.io/topic/85833/when-is-time-to-include-moc_xxx-cpp-at-bottom-of-a-cpp-fileerror: undefined reference to vtable for Dialog' error: undefined reference toDialog::staticMetaObject'
Qt's Meta-Object Compiler (moc, https://doc.qt.io/qt-5/moc.html ) needs to process all classes that inherit
QObject
and contain theQ_OBJECT
macro.This error occured because moc did not detect and process your
Dialog
class.moc can automatically detect classes that are defined in .h files. But if you define your class in a .cpp file, then you must write
#include "moc_XXX.cpp"
to tell moc to process the .cpp file.