Results 1 to 2 of 2

Thread: Compiling QT4 Code using cmake 2.6

  1. #1
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Compiling QT4 Code using cmake 2.6

    Hi Guys!

    I have a big Problem. I want to compile my Source Code with Cmake, because later I must add another lib and that isn't possible with qmake.

    First here the Code:
    widget.h
    Qt Code:
    1. #define _WIDGET_H_
    2.  
    3. #include <QApplication>
    4. #include <QFont>
    5. #include <QPushButton>
    6. #include <QWidget>
    7. #include <QtGui>
    8.  
    9.  
    10. class MyWidget : public QWidget{
    11. Q_OBJECT
    12. public:
    13. MyWidget(QWidget *parent = 0);
    14. private:
    15. QLineEdit *lineEdit_v;
    16. QTableWidget *tablewidget;
    17. QLineEdit *lineEdit_l;
    18. QLineEdit *lineEdit_ru;
    19. int richt[3][200];
    20. int counter;
    21. //QLabel *bla;
    22. //QPushButton *subsampling;
    23. //QLabel *fertig;
    24.  
    25. public slots:
    26.  
    27. void copy_list();
    28. };
    To copy to clipboard, switch view to plain text mode 


    widget.cpp:
    Qt Code:
    1. #include "widget.h"
    2. #include <QtGui>
    3. #include <QHeaderView>
    4. #include <QTableWidget>
    5.  
    6. MyWidget::MyWidget(QWidget *parent)
    7. : QWidget(parent)
    8. {
    9.  
    10. //Attribute
    11. counter=0;
    12. lineEdit_v = new QLineEdit();
    13. lineEdit_l = new QLineEdit();
    14. lineEdit_ru = new QLineEdit();
    15.  
    16. QPushButton *send = new QPushButton("Ab in die Liste");
    17.  
    18. tablewidget = new QTableWidget(30, 3, this);
    19.  
    20. QLabel *label_l = new QLabel(tr("Drehung:"));
    21. QLabel *label_ru = new QLabel(tr("Dauer:"));
    22. QLabel *label_v = new QLabel(tr("Speed:"));
    23.  
    24. //Attributen-Zuweisungen
    25. lineEdit_v->setMaximumSize( 210, lineEdit_v->maximumSize().height() );
    26. lineEdit_l->setMaximumSize( 210, lineEdit_l->maximumSize().height() );
    27. lineEdit_ru->setMaximumSize( 210, lineEdit_ru->maximumSize().height() );
    28.  
    29. //Anpassung des QTableWidgets
    30. //
    31. //Ueberschrift
    32. QStringList fonts;
    33. fonts << "Speed" << "Drehung" << "Dauer";
    34. tablewidget->setHorizontalHeaderLabels(fonts);
    35.  
    36. //SIGNALS AND SLOTS
    37.  
    38. connect(send, SIGNAL(clicked()),this , SLOT(copy_list()));
    39.  
    40. //LAYOUT
    41.  
    42. QVBoxLayout *drehung = new QVBoxLayout();
    43. drehung->addWidget(label_l);
    44. drehung->addWidget(lineEdit_l);
    45.  
    46. QVBoxLayout *vor = new QVBoxLayout();
    47. vor->addWidget(label_v);
    48. vor->addWidget(lineEdit_v);
    49.  
    50. QVBoxLayout *dauer = new QVBoxLayout();
    51. dauer->addWidget(label_ru);
    52. dauer->addWidget(lineEdit_ru);
    53.  
    54. QVBoxLayout *linkespalte = new QVBoxLayout();
    55. linkespalte->addLayout(vor);
    56. linkespalte ->addLayout(drehung);
    57. linkespalte->addLayout(dauer);
    58. linkespalte->addWidget(send);
    59.  
    60. QVBoxLayout *center = new QVBoxLayout();
    61. center->addLayout(linkespalte);
    62. center->addStretch();
    63.  
    64. QHBoxLayout *end = new QHBoxLayout();
    65. end->addLayout(center);
    66. end->addWidget(tablewidget);
    67.  
    68.  
    69.  
    70.  
    71. this->setLayout(end);
    72.  
    73.  
    74. }
    75.  
    76.  
    77.  
    78. void MyWidget::copy_list(){
    79.  
    80.  
    81. QString speed = lineEdit_v->text();
    82. QString dreh = lineEdit_l->text();
    83. QString dauer = lineEdit_ru->text();
    84.  
    85. QTableWidgetItem *newItem = new QTableWidgetItem(speed);
    86. QTableWidgetItem *degree = new QTableWidgetItem(dreh);
    87. QTableWidgetItem *time = new QTableWidgetItem(dauer);
    88.  
    89. tablewidget->setItem(counter, 0, newItem);
    90. tablewidget->setItem(counter, 1, degree);
    91. tablewidget->setItem(counter, 2, time);
    92.  
    93. counter++;
    94. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "widget.h"
    2. #include <QtGui>
    3. #include <QHeaderView>
    4. #include <QTableWidget>
    5.  
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10. MyWidget widget;
    11. widget.show();
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    And at last but not least the CMakeLists.txt

    Qt Code:
    1. cmake_minimum_required(VERSION 2.6)
    2.  
    3.  
    4. FIND_PACKAGE(Qt4)
    5. SET(QT_USE_QTXML 1)
    6. INCLUDE(${QT_USE_FILE})
    7. QT4_AUTOMOC(main.cpp widget.cpp widget.h)
    8. ADD_EXECUTABLE(myexe main.cpp widget.cpp)
    9. TARGET_LINK_LIBRARIES(myexe ${QT_LIBRARIES})
    To copy to clipboard, switch view to plain text mode 

    I have all the files in a folder. In the console, I am typing:

    "cmake-2.6 ."
    Qt Code:
    1. -- The C compiler identification is GNU
    2. -- The CXX compiler identification is GNU
    3. -- Check for working C compiler: /usr/bin/gcc
    4. -- Check for working C compiler: /usr/bin/gcc -- works
    5. -- Detecting C compiler ABI info
    6. -- Detecting C compiler ABI info - done
    7. -- Check for working CXX compiler: /usr/bin/c++
    8. -- Check for working CXX compiler: /usr/bin/c++ -- works
    9. -- Detecting CXX compiler ABI info
    10. -- Detecting CXX compiler ABI info - done
    11. -- Looking for Q_WS_X11
    12. -- Looking for Q_WS_X11 - found
    13. -- Looking for Q_WS_WIN
    14. -- Looking for Q_WS_WIN - not found.
    15. -- Looking for Q_WS_QWS
    16. -- Looking for Q_WS_QWS - not found.
    17. -- Looking for Q_WS_MAC
    18. -- Looking for Q_WS_MAC - not found.
    19. -- Found Qt-Version 4.4.2
    20. -- Found OpenSSL: /usr/lib/libssl.so
    21. -- Looking for _POSIX_TIMERS
    22. -- Looking for _POSIX_TIMERS - found
    23. -- Configuring done
    24. -- Generating done
    25. -- Build files have been written to: /home/serenity/RoboGui
    To copy to clipboard, switch view to plain text mode 

    okay, all fine until now.
    then I must make it:

    typing:
    "make"
    Qt Code:
    1. Scanning dependencies of target myexe
    2. [ 50%] Building CXX object CMakeFiles/myexe.dir/main.cpp.o
    3. [100%] Building CXX object CMakeFiles/myexe.dir/widget.cpp.o
    4. Linking CXX executable myexe
    5. CMakeFiles/myexe.dir/main.cpp.o: In function `MyWidget::~MyWidget()':
    6. main.cpp:(.text._ZN8MyWidgetD1Ev[MyWidget::~MyWidget()]+0xf): undefined reference to `vtable for MyWidget'
    7. main.cpp:(.text._ZN8MyWidgetD1Ev[MyWidget::~MyWidget()]+0x19): undefined reference to `vtable for MyWidget'
    8. CMakeFiles/myexe.dir/widget.cpp.o: In function `MyWidget::MyWidget(QWidget*)':
    9. widget.cpp:(.text+0x2ef): undefined reference to `vtable for MyWidget'
    10. widget.cpp:(.text+0x2f9): undefined reference to `vtable for MyWidget'
    11. CMakeFiles/myexe.dir/widget.cpp.o: In function `MyWidget::MyWidget(QWidget*)':
    12. widget.cpp:(.text+0xf1b): undefined reference to `vtable for MyWidget'
    13. CMakeFiles/myexe.dir/widget.cpp.o:widget.cpp:(.text+0xf25): more undefined references to `vtable for MyWidget' follow
    14. CMakeFiles/myexe.dir/widget.cpp.o: In function `MyWidget::tr(char const*, char const*)':
    15. widget.cpp:(.text._ZN8MyWidget2trEPKcS1_[MyWidget::tr(char const*, char const*)]+0x1e): undefined reference to `MyWidget::staticMetaObject'
    16. collect2: ld gab 1 als Ende-Status zurück
    17. make[2]: *** [myexe] Fehler 1
    18. make[1]: *** [CMakeFiles/myexe.dir/all] Fehler 2
    19. make: *** [all] Fehler 2
    To copy to clipboard, switch view to plain text mode 

    Where is the problem and why couldn't I compile my program? ("Fehler" means error)

    I also testet it with qmake and it works. But I have to do it with cmake.

    Greetings
    Serenity

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Compiling QT4 Code using cmake 2.6

    I don't know the effect of QT4_AUTOMOC call but in CMake docs I've found
    macro QT4_AUTOMOC(sourcefile1 sourcefile2 ... )
    This macro is still experimental.
    It can be used to have moc automatically handled.
    So if you have the files foo.h and foo.cpp, and in foo.h a
    a class uses the Q_OBJECT macro, moc has to run on it. If you don't
    want to use QT4_WRAP_CPP() (which is reliable and mature), you can insert
    #include "foo.moc"
    in foo.cpp and then give foo.cpp as argument to QT4_AUTOMOC(). This will the
    scan all listed files at cmake-time for such included moc files and if it finds
    them cause a rule to be generated to run moc at build time on the
    accompanying header file foo.h.
    If a source file has the SKIP_AUTOMOC property set it will be ignored by this macro.
    You have to put
    Qt Code:
    1. #include "widget.moc"
    To copy to clipboard, switch view to plain text mode 
    in the widget.cpp file and remove widget.h as parameter of QT4_AUTOMOC

    See http://http://wiki.qtcentre.org/inde...pps_with_CMake
    that suggest the use of QT4_WRAP_CPP instead of QT4_AUTOMOC.

    In your widget.h file you have to insert
    Qt Code:
    1. #ifndef _WIDGET_H_
    To copy to clipboard, switch view to plain text mode 
    as first line and
    Qt Code:
    1. #endif // _WIDGET_H_
    To copy to clipboard, switch view to plain text mode 
    as last line of code
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. copyright and gpl
    By janus in forum General Discussion
    Replies: 8
    Last Post: 21st October 2008, 01:13
  2. Qt Cryptographic Architecture
    By vermarajeev in forum Qt Programming
    Replies: 6
    Last Post: 9th February 2007, 13:15
  3. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  4. CMake and pthreads
    By Matt Smith in forum General Discussion
    Replies: 5
    Last Post: 3rd September 2006, 20:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.