I'm following Qt's Qt for Beginners tutorial and for some reason I am getting a compilation error when trying to build the simplest example of a gui. It looks like it's failing on button creation because it thinks that qcolor is unimplemented(?). I just downloaded the latest Qt 5.14.0 and installed it today, so it's possible that something happened during the install?
This is what my project layout looks like:
build-new_qt_project-Desktop_Qt_5_14_0_GCC_64bit-Debug:
Makefile
new_qt_project:
main.cpp
new_qt_project.pro
main.cpp:
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
{
QApplication app (argc, argv);
QPushButton button ("Hello world !");
button.show();
return app.exec();
}
new_qt_project.pro:
TEMPLATE = app
TARGET = new_qt_project
QT = core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += main.cpp
And this is the output from trying to make it:
In file included from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qpixmap.h:45:0,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qicon.h:46,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtWidgets/qabstractbutton.h:44,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtWidgets/qpushbutton.h:44,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtWidgets/QPushButton:1,
from ../new_qt_project/main.cpp:2:
../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qcolor.h: In constructor ‘constexpr QColor::QColor(int, int, int, int)’:
../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qcolor.h:79:18: sorry, unimplemented: use of the value of the object being constructed in a constant expression
0) {}
^
make: *** [main.o] Error 1
I'm using the default (detected) kit to build it and it has the C++ compiler pointing to the one in /usr/bin, so I think that is correct. Is there something additional I've missed?
I think this is the problem:
Qt bug
update your gcc. up to 4.9 !!!
i had this problem and i used gcc 4.8 and after update it to gcc-7 it is now correct
Related
I have a Qt Widget project that I created using QtCreator and Qt version 5.15.2 to which I'm trying to add the QtVirtualKeyboard as matchbox-keyboard I've already tried using stays under the application when it's in fullscreen mode.
However I'm having trouble getting it to work as it's not appearing at all at the moment. This is how I've tried adding it
Main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
MainWindow w;
//w.showFullScreen();
w.show();
return a.exec();
}
I've tried adding QT += qtvirtualkeyboard or QT += virtualkeyboard in the .pro file but it just gives me an error saying "unknown module"
How can I add the virtual keyboard to the project?
You will need to make sure that the QtVirtualKeyboard library is selected during the installation process.
Also, I would recommend you that you start using cmake for Qt applications as Qt has officially dropped qmake since the release of Qt 6.
It is not to say that you cannot use it, but you will get better support and results by using an actively developed build system rather than an abandoned.
When trying to use some Qt-5 classes I'm experiencing crashes. I first discovered this trying to use QFileSystemModel. Trying to call setRootPath immediately leads to a crash. The callstack isn't of much help (all of it is assembly code) except that QFileIconProvider::icon() is the last function called before the seg fault occurs.
So next I tried using QFileIconProvider manually and -to no surprise- it also crashed the program.
I'm using QtCreator 4 and the type of project is "Qt Quick Application". When I instead create a project of type "Qt Widgets Application", I can use both QFileIconProvider and QFileSystemModel without problems.
Here's where I'm out of ideas. I don't know enough about the Qt environment to know what difference between the two types of projects could lead to the seg fault.
Both projects use the same Kit (same gcc, same Qt 5.6.1) and the default settings as set by QtCreator.
This is my project.pro file:
TEMPLATE = app
QT += qml quick widgets //default .pro file except for widgets
CONFIG += c++11
SOURCES += main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
This is main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDir>
#include <QFileSystemModel>
#include <QQmlContext>
#include <QFileIconProvider>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
//If trying to use QFileSystemMode...
QFileSystemModel model;
model.setRootPath("/somefolder/"); //..the crash happens here
//Attempting to use QFileIconProvider also crashes
//QFileIconProvider fip;
//fip.icon( QFileInfo("/somefolder/somefile") ); //<- here
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
I'd appreciate any help or pointers as to how to debug that mess.
As confusing as it may sound, QFileSystemModel is part of QtWidgets and therefore requires you to create and instance of QApplication instead of QGuiApplication.
I just started using Qt. I have a problem with compiling the first example.
main.cpp:
#include <QCoreApplication>
#include <QPushButton>
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QPushButton button ("Hello world !");
return app.exec();
}
CMake.txt:
cmake_minimum_required(VERSION 2.6)
project(new)
find_package(Qt4 REQUIRED)
enable_testing()
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
set(source_SRCS main.cpp)
qt4_automoc(${source_SRCS})
add_executable(new ${source_SRCS})
target_link_libraries(new${QT_QTCORE_LIBRARY})
add_subdirectory(tests)
install(TARGETS new RUNTIME DESTINATION .)
The error I get upon building is:
undefined reference to `QPushButton::QPushButton(QString const&,QWidget*)'
It is a linking problem, but how can I solve it?
Here is what I think you are missing:
find_package(Qt4 REQUIRED QtGui)
looking at your cmake you probably want to change the target_link_libraries for the following:
target_link_libraries(new ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})
You have three problems:
You're not linking with the Gui module (Widgets module in Qt 5). This is covered in the other answer.
You you must use QApplication in widgets-based applications. Since QPushButton comes from the Gui module (Widgets in Qt5), you can't merely use QCoreApplication nor QGuiApplication: your program will crash as soon as you attempt to instantiate a QWidget.
You're not showing the button, so when your program starts you'll see nothing once you fix the above.
Your main.cpp should look like:
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
#include <QtGui>
#else
#include <QtWidgets>
#endif
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QPushButton button ("Hello world !");
button.show();
return app.exec();
}
You're also going to need to link against the QtGui and QtWidgets library. Within qmake it handles which of the many libraries make up Qt, you'll have to do this by hand in cmake.
If you look at the documentation for QPushButton (http://doc.qt.io/qt-5/qpushbutton.html), the "qmake" line shows what library you need.
Consider using qmake instead of cmake with Qt.
Anyway, QCoreApplication (see docs) is the console version of main application class and will not work in GUI application. QPushButton is a widget class and can exist alone and will make a window (although you must show() it explicitely for that) but only with QApplication.
When using qmake in your *.pro file you need to include widgets like so:
CONFIG += widgets
and make sure you don't have
CONFIG -= gui
If you insist on using cmake then see here.
this answer solves my same problem :)
ok,
i had can solve it by myself.
For all they have they problem too:
The error is sourced from the use of "Q_OBJECT".
To solve the error, right-cklick on the Project and choose "Run qmake" and after >this: "Rebuild".
Then the error should be disappeared ;-)
-casisto
https://forum.qt.io/topic/52439/get-undefined-reference-error-but-don-t-know-why/2
I have downloaded and installed Qt and I'm learning how to use it.
So, I created a new project myfristqt (empty project). I then added a main.cpp file with this code:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
return app.exec();
}
First thing I noticed #include <QApplication> line is hightlighted with a red line as if QApplication was an unknown identifier. So, I compiled it to see what happens and here's the error I got:
(internal):1: error : Using OS scope before setting MAKEFILE_GENERATOR
Any idea why this is happening ? I'm using Windows XP
MAKEFILE_GENERATOR is a qmake variable.
This variable contains the name of the Makefile generator to use when generating a Makefile. The value of this variable is typically handled internally by qmake and rarely needs to be modified.
It define in QTDIR/mkspecs/PLATFORM/qmake.conf. Where PLATFORM is maybe cygwin-g++, win32-msvc200x on your Windows XP.
I started to work for my homework using QT creator to make the GUI, but I gt this error and I can't manage to find the reason for it, nor can I understand what it means. I suppose it sees my main function twice but I do not know why... please assist me in fixing this error:
error:
Makefile.Debug:155: warning: overriding commands for target `debug/main.o'
Makefile.Debug:142: warning: ignoring old commands for target `debug/main.o'
debug/main.o: In function `Z5qMainiPPc':
D:\c++\Labs\GUI_r/../../../info/qt/Desktop/Qt/4.8.1/mingw/include/QtGui/qwidget.h:494: multiple definition of `qMain(int, char**)'
debug/main.o:D:\c++\Labs\GUI_r/main.cpp:7: first defined here
collect2: ld returned 1 exit status
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "controller.h"
#include "StudentRepository.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StudentRepository *stre = new StudentRepository();
Controller *c = new Controller(stre);
MainWindow w(c);
w.show();
return a.exec();
}
edit: long code removed - not the reason for the error. Check the answere it is useful.
The reason for that linking error is because of awkawrd behaivior behalf QT creator. I had in the projectName.pro -
QT += core gui
TARGET = GUI_r
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
main.cpp \ /////// Double call of main.cpp
StudentRepository.cpp \
controller.cpp
HEADERS += mainwindow.h \
controller.h \
StudentRepository.h \
Student.h \
ui_mainwindow.h \ /////Double call of ui_mainwindow.h
ui_mainwindow.h
FORMS += mainwindow.ui
Thank you, i hope this post will be usefull to other new users of QTcreator.
Maybe your project contains another source file with a main. Somewhere files duplicated. Check "SOURCES =" and main.cpp in your .pro file.
You can only have one QApplication per program!
Review your classes (Controller, StudentRepository, MainWindow) and make sure that they are not declaring QApplication as well.
It does see two definitions of qMain , not your main.
You have probably taken a sample program and modified it by adding your code. Recreate those steps and see when it stopped working.
When writing a code, do a compilation as often as possible, to find such errors right after you've introduced them.