Can't identify why the code can't be executed [closed] - qt

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I've just started with Qt. This is an example from a book. I've tried to compare the code with that in the book. It seems identical.
I am getting errors. How can I fix them?
The NetBeans shows as follows:
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE=/C/Qt/4.8.4/bin/qmake.exe SUBPROJECTS= .build-conf
make[1]: Entering directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
/C/Qt/4.8.4/bin/qmake.exe VPATH=. -spec win32-g++ -o qttmp-Debug.mk nbproject/qt-Debug.pro
mv -f qttmp-Debug.mk nbproject/qt-Debug.mk
"/usr/bin/make" -f nbproject/qt-Debug.mk dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe
make[2]: Entering directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
g++ -mthreads -Wl,-subsystem,windows -o dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe build/Debug/MinGW_Qt-Windows/main.o build/Debug/MinGW_Qt-Windows/moc_Counter.o -L'c:/Qt/4.8.4/lib' -lmingw32 -lqtmaind build/Debug/MinGW_Qt-Windows/HelloWorld1_resource_res.o -lQtGuid4 -lQtCored4
build/Debug/MinGW_Qt-Windows/main.o: In function `Z5qMainiPPc':
C:\Documents and Settings\Deloitte\��� ���������\NetBeansProjects\HelloWorld1/main.cpp:16: undefined reference to `Counter::Counter()'
build/Debug/MinGW_Qt-Windows/moc_Counter.o: In function `ZN7Counter18qt_static_metacallEP7QObjectN11QMetaObject4CallEiPPv':
C:\Documents and Settings\Deloitte\��� ���������\NetBeansProjects\HelloWorld1/moc_Counter.cpp:56: undefined reference to `Counter::slotInc()'
collect2: ���������� ld ����������� � ����� �������� 1
make[2]: *** [dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe] Error 1
make[2]: Leaving directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
counter.h
#ifndef _Counter_h_
#define _Counter_h_
#include <QObject>
class Counter:public QObject{
Q_OBJECT
private:
int m_nValue;
public:
Counter();
public slots:
void slotInc();
signals:
void goodbye();
void counterChanged(int);
};
main.cpp
#include <QtGui/QApplication>
#include <QtGui>
#include "Counter.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QLabel lbl("0");
QPushButton cmd ("ADD");
Counter counter;
lbl.show();
cmd.show();
QObject::connect(&cmd, SIGNAL(clicked()),
&counter, SLOT (slotInc()));
QObject::connect(&counter, SIGNAL(counterChanged(int)),
&lbl, SLOT(setNum(int)));
QObject::connect(&counter, SIGNAL (goodbye()), &app, SLOT(quit()));
return app.exec();
}

Your code is missing the actual definition for the Counter class member functions, notably Counter::Counter the constructor and Counter::slotInc. You must provide implementations for them.
Counter::goodbye and Counter::counterChanged are signals for which the definition is provided by the Qt MOC.

Related

How to run qt creator demo code in clion?

To see the code:
I create a repository on https://github.com/jaysimon/clion_qtcreator
I successed make a demo ui in qt creator, but failed in clion with the same code.
I got this:
[ 16%] Automatic MOC and UIC for target 20190314_clion_qt
[ 16%] Built target 20190314_clion_qt_autogen
[ 33%] Linking CXX executable 20190314_clion_qt
CMakeFiles/20190314_clion_qt.dir/src/mainwindow.cpp.o: In function `MainWindow::MainWindow(QWidget*)':
/home/hw/01-workspace/20190314_clion_qt/src/mainwindow.cpp:7: undefined reference to `vtable for MainWindow'
/home/hw/01-workspace/20190314_clion_qt/src/mainwindow.cpp:7: undefined reference to `vtable for MainWindow'
CMakeFiles/20190314_clion_qt.dir/src/mainwindow.cpp.o: In function `MainWindow::~MainWindow()':
/home/hw/01-workspace/20190314_clion_qt/src/mainwindow.cpp:30: undefined reference to `vtable for MainWindow'
/home/hw/01-workspace/20190314_clion_qt/src/mainwindow.cpp:30: undefined reference to `vtable for MainWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/20190314_clion_qt.dir/build.make:131: recipe for target '20190314_clion_qt' failed
make[3]: *** [20190314_clion_qt] Error 1
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/20190314_clion_qt.dir/all' failed
make[2]: *** [CMakeFiles/20190314_clion_qt.dir/all] Error 2
CMakeFiles/Makefile2:84: recipe for target 'CMakeFiles/20190314_clion_qt.dir/rule' failed
make[1]: *** [CMakeFiles/20190314_clion_qt.dir/rule] Error 2
Makefile:118: recipe for target '20190314_clion_qt' failed
make: *** [20190314_clion_qt] Error 2
Could you help to run it with CMake successfully?
A push request is the best.
CMakeList.txt:
cmake_minimum_required(VERSION 3.0)
project(20190314_clion_qt)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)
include_directories(./inc)
set(SOURCE_CODE
src/main.cpp
src/mainwindow.cpp
src/deal.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_CODE})
target_link_libraries(${PROJECT_NAME} -pthread Qt5::Core)
target_link_libraries(${PROJECT_NAME} Qt5::Gui)
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)
main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include "deal.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
pthread_t tid;
pthread_create(&tid, NULL, run, NULL);
//pthread_exit(&tid);
return a.exec();
}
I solved it, but confused why, here is the solution, you can also git it on github.
put MainWindow.cpp MainWindow.h MainWindow.ui, all 3 files into the same directory, it's solved.

Separate class for Qt app

Under Fedora 27 Linux I want to try out a simple Hello World example with Qt and C++. I've created a separate GUI class which is populated with a label and a button. The init macro “Q_OBJECT” was commented out, because it produces an error message. ("undefined reference to vtable for MainWindow") After compiling the sourcecode, a window is shown on the screen, but the label and the button is missing. I mean, the compilation process works, the gui starts, but the result is not as expected.
If i put all the commands in the main-function without creating a separate class, Qt works great. The problem is to define a separate class which inherits from QMainWindow. Most of the example tutorials out there are working with Qt-Creator, but i want to do it from scratch on the command line level. Any hints are welcome.
// compile: clang++ -std=c++14 -lQtCore -lQtGui file.cpp
#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtGui/QTextEdit>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <iostream>
class MainWindow : public QMainWindow {
//Q_OBJECT
public:
QWidget window;
MainWindow() {
window.resize(500, 400);
QLabel label1( "input:" , &window);
QPushButton button ("run", &window);
button.move(0,300);
}
void show() {
window.show();
}
};
int main(int argc, char **argv)
{
QApplication app (argc, argv);
MainWindow mywindow;
mywindow.show();
return app.exec();
}
QLabel label1 and QPushButton button are local variables in the constructor MainWindow::MainWindow(). Hence, when constructor returns they are going out of scope and are destroyed/deleted. You have to make them member variables. (This is actually a C++ issue.)
Additionally, I recommend to learn about layouting in Qt. Qt doc. provides examples e.g. Basic Layouts Example
Here is an even smaller I composed by modifying the OP:
testQMainWindow.cc:
#include <QtWidgets>
class MainWindow: public QMainWindow {
private:
QWidget central;
QHBoxLayout hBox;
QLabel label;
QPushButton button;
public:
MainWindow();
};
MainWindow::MainWindow():
hBox(this),
label("input:", this),
button("run", this)
{
hBox.addWidget(&label, 1);
hBox.addWidget(&button, 0);
central.setLayout(&hBox);
setCentralWidget(&central);
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWindow win;
win.show();
return app.exec();
}
testQMainWindow.pro:
SOURCES = testQMainWindow.cc
QT += widgets
Compile and test:
$ qmake-qt5 testQMainWindow.pro
$ make
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQMainWindow.o testQMainWindow.cc
g++ -o testQMainWindow.exe testQMainWindow.o -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
$ ./testQMainWindow
I'm working on Windows 10. I did the sample in cygwin which is the closest to Linux I have at hand.
i want to do it from scratch on the command line level
In this case you either have to take care for all the necessary additional build steps, includes and linkage yourself, as Dmitry already hinted in a comment. Or you just use qmake, which is highly recommended, because it takes care of all the Qt specific stuff.

Emacs embedded in a Qt Application

I've tried to embed emacs in a Qt Application using QX11EmbedContainer, and works but with two important exception. First of all, here is the code:
#include <QX11EmbedWidget>
#include <QtGui>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QX11EmbedContainer container;
container.show();
container.resize(500, 500);
QProcess* process = new QProcess(&container);
QString executable("emacsclient");
QStringList arguments;
arguments << "--parent-id" << QString::number(container.winId());
process->start(executable, arguments);
int status = app.exec();
process->close();
return status;
}
And the compilation and execution line (and the previous thrown of the emacs server):
$ emacs -q --daemon &
// filtered output
$ g++ test.cpp -lQtGui -lQtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4
$ ./a.out
And finally, the result:
But, when or if I try to write something in the minibuffer, the size of the widget is collapsed, and the focus is also lost:
If I make click in the (now shorter) widget, I can continue working with emacs without problems, but I should resize the window in order to emacs is expanded other time as originally.
Where is the problem?
Try using a layout.
Here is the Qt5 documentation on layout management.

Using QApplication with command line arguments

QApplication::QApplication ( int & argc, char ** argv )
Initializes the window system and constructs an application object
with argc command line arguments in argv.
Warning: The data referred to by argc and argv must stay valid for the
entire lifetime of the QApplication object. In addition, argc must be
greater than zero and argv must contain at least one valid character
string.
From this link: http://doc.qt.io/qt-4.8/qapplication.html#QApplication
What can be the arguments to the executable file? Any examples?
I tried specifying something like:
anisha#linux-dopx:~/Desktop/notes/qt> make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../qtsdk-2010.05/qt/mkspecs/linux-g++-64 -I. -I../../../qtsdk-2010.05/qt/include/QtCore -I../../../qtsdk-2010.05/qt/include/QtGui -I../../../qtsdk-2010.05/qt/include -I. -I. -o widgets.o widgets.cpp
g++ -m64 -Wl,-O1 -Wl,-rpath,/home/anisha/qtsdk-2010.05/qt/lib -o qt widgets.o -L/home/anisha/qtsdk-2010.05/qt/lib -lQtGui -L/home/anisha/qtsdk-2010.05/qt/lib -L/usr/X11R6/lib64 -lQtCore -lpthread
anisha#linux-dopx:~/Desktop/notes/qt> ./qt 2 f g
anisha#linux-dopx:~/Desktop/notes/qt>
Nothing special happened, nor I knew what I was doing or what I was supposed to do.
EDIT 1: The code on which I tried the ./qt -style=windows.
#include <QtGui>
int main (int argc, char *argv[])
{
QApplication app (argc, argv);
QWidget objQWidget;
objQWidget.show ();
objQWidget.resize (320, 240);
objQWidget.setWindowTitle ("Text to be shown on the title bar\n");
// Adding a "child" widget.
QPushButton *objQPushButton = new QPushButton ("Text to be shown on the button", &objQWidget);
objQPushButton->move (100, 100);
objQPushButton->show ();
return app.exec ();
}
The arguments passed in the constructor are later accessible through the static method
QStringList QCoreApplication::arguments(). By this, command line arguments can be handled everywhere in your code.
Continue reading that documentation. The set of flags QApplication acts on is listed there.
Try for example:
./qt -style=windows
The arguments that QApplication doesn't deal with are simply left alone. The ones it does process are removed (which is why that function takes non-const arguments).
The suggestion about using QCoreApplication is only recommended of you have a console application. If you are using a QApplication instead, and want to access command-line arguments from inside a QWidget, you can do it with the global pointer qApp:
Here you can find the documentation from Nokia, or here from qt-project.org . In the documentation browser of Qt Creator I couldn't find it, so it is at best not that easily accessible.
so you can find:
int my_argc = qApp->arguments().count();
QString my_argv_0 = qApp->arguments.at(0);
...
and so on.
I know this question is old, but took me some time to find a way to do it from within my Main Window, so hope this helps someone else.
Thanks, Dissident penguin! This helped me a lot!
Just note that:
QString my_argv_0 = qApp->arguments.at(0);
should be replaced with:
QString my_argv_0 = qApp->arguments().at(0);
(note the additional () after 'arguments')

Problems on Qt on NetBeans

After having solved a Qt configured problem on my system, I have now installed NetBeans and everything is OK. It is just that I have the following problem. Here is the code:
#include <QtGui/QApplication>
#include <QDir>
#include <QFileInfo>
#include <QtDebug>
int main(int argc, char **argv) {
foreach(QFileInfo drive,QDir::drives()){
qDebug()<<"Drive: "<<drive.absolutePath();
QDir dir=drive.dir();
dir.setFilter(QDir::Dirs);
foreach(QFileInfo rootDirs,dir.entryInfoList())
qDebug()<< " "<<rootDirs.fileName();
}
return 0;
// return app.exec();
}
and errors are
/usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/david/NetBeansProjects/QtApplication_1'
/usr/bin/qmake VPATH=. -o qttmp-Debug.mk nbproject/qt-Debug.pro
mv -f qttmp-Debug.mk nbproject/qt-Debug.mk
/usr/bin/make -f nbproject/qt-Debug.mk dist/Debug/GNU-Linux-x86/QtApplication_1
make[2]: Entering directory `/home/david/NetBeansProjects/QtApplication_1'
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -Inbproject -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -Inbproject -I. -o build/Debug/GNU-Linux-x86/Qt1.o Qt1.cpp
Qt1.cpp:7: warning: unused parameter ‘argc’
Qt1.cpp:7: warning: unused parameter ‘argv’
g++ -o dist/Debug/GNU-Linux-x86/QtApplication_1 build/Debug/GNU-Linux-x86/Qt1.o build/Debug/GNU-Linux-x86/main.o -L/usr/lib -lQtGui -lQtCore -lpthread
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/david/NetBeansProjects/QtApplication_1/main.cpp:10: multiple definition of `main'
build/Debug/GNU-Linux-x86/Qt1.o:/home/david/NetBeansProjects/QtApplication_1/Qt1.cpp:7: first defined here
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/QtApplication_1] Error 1
make[2]: Leaving directory `/home/david/NetBeansProjects/QtApplication_1'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/david/NetBeansProjects/QtApplication_1'
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
How can I fix this problem?
According to that output, you are compiling two source files Qt1.cpp and main.cpp, both of which define the function main(). You need to remove one of those two files from your project or remove the definition of main() from one of them.
As an added step to get rid of the warning about unused parameters in main(), change it to
main(int /*argc*/, char **/*argv*/)
until you need to use those arguments.

Resources