‘class QApplication’ has no member named ‘setMainWidget’ - c++

I'm attempting to compile a "Hello World!" application using Qt but on compilation I'm getting the following error: ‘class QApplication’ has no member named ‘setMainWidget’ and I'm not sure why, my source file looks like:
/****************************************************************
**
** Qt tutorial 1
**
****************************************************************/
#include <qapplication.h>
#include <qpushbutton.h>
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QPushButton hello( "Hello world!", 0 );
hello.resize( 100, 30 );
a.setMainWidget( &hello );
hello.show();
return a.exec();
}
and I created the Makefile with cmake using following CMakeLists.txt:
cmake_minimum_required(VERSION 3.1.0)
project(hello_world)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed
set(CMAKE_AUTOMOC ON)
# Create code from a list of Qt designer ui files
#set(CMAKE_AUTOUIC ON)
# Find the QtWidgets library
find_package(Qt5 COMPONENTS Widgets REQUIRED)
# Populate a CMake variable with the sources
set(hello_world_SRCS
main.cpp
)
# Tell CMake to create the helloworld executable
add_executable(hello_world WIN32 ${hello_world_SRCS})
# Use the Widgets module from Qt 5
target_link_libraries(hello_world Qt5::Widgets)
What am I doing wrong?

Related

Qt plugin initialization failure upon starting Windows application

I could not start this simple startup code.
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
return app.exec();
}
The following popup appears:
The bin folder looks like this:
I build the project with the following Conan script:
...
options = {'qt6': 'ANY'}
default_options = {'qt:shared': True, 'qt6': None}
generators = 'cmake', 'cmake_paths', 'cmake_find_package', 'cmake_find_package_multi'
requires = 'qt/6.2.2'
...
My CMakeLists.txt is the following:
...
# Find Qt package.
find_package(Qt6 REQUIRED COMPONENTS Widgets Core)
# Find includes in the build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_VERBOSE_MAKEFILE TRUE)
# Turn on automatic invocation of the MOC, UIC & RCC
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
# Link the thirdparties.
target_link_libraries(${PROJECT_NAME} Qt6::Widgets Qt6::Core)
...
May I ask a suggestion what am I doing wrong?

glibmm.h: could not find such directory or file

I want to include gtk to my new cmake project. But wen I compile, I get this error: +
In file included from /home/chhu/CLionProjects/MasterMind/main.cpp:2:
/usr/include/gtkmm-3.0/gtkmm.h:87: fatal error: glibmm.h: could not find such directory or file
This is my cmake file:
cmake_minimum_required(VERSION 3.17)
project(MasterMind)
set(CMAKE_CXX_STANDARD 14)
# Use the package PkgConfig to detect GTK+ headers/library files
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(GTK3 REQUIRED gtk+-3.0)
# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
INCLUDE_DIRECTORIES(${GTK3_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTK3_LIBRARY_DIRS})
# Add other flags to the compiler
ADD_DEFINITIONS(${GTK3_CFLAGS_OTHER})
add_executable(MasterMind main.cpp)
# Link the target to the GTK+ libraries
TARGET_LINK_LIBRARIES(MasterMind ${GTK3_LIBRARIES})
This is my class:
#include <gtkmm-3.0/gtkmm.h>
int main(int argc, char *argv[]) {
auto app =
Gtk::Application::create(argc, argv,
"org.gtkmm.examples.base");
Gtk::Window window;
window.set_default_size(200, 200);
return app->run(window);
}
I am developing under ubuntu 20.4. My g++ version is 9.3.0.
Gtkmm is not a part of Gtk. You should update your CMakeLists
PKG_CHECK_MODULES(GTKMM3 REQUIRED gtkmm-3.0)
INCLUDE_DIRECTORIES(${GTKMM3_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTKMM3_LIBRARY_DIRS})
ADD_DEFINITIONS(${GTKMM3_CFLAGS_OTHER})
TARGET_LINK_LIBRARIES(MasterMind ${GTKMM3_LIBRARIES})

Getting reference issue in Qt while implementing OpenSceneGraph

I am getting error (**
**> CMakeFiles\untitled3.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x46):
undefined reference to `_imp___ZN9osgViewer6ViewerC1Ev'**)
while adding OpenSceneGraph in Qt. My profile is cmakelist.txt.
cmake_minimum_required(VERSION 2.8.12)
project(untitled3)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Core)
FIND_PATH(OPENSCENEGRAPH_INCLUDE_DIR osg/Referenced
PATHS
$ENV{OSG_ROOT}/include
$ENV{OSG_DIR}/include
/usr/include
/usr/local/include
)
FIND_PATH(OPENSCENEGRAPH_LIB_DIR libosg.so osg.lib
PATHS
$ENV{OSG_ROOT}/lib
$ENV{OSG_DIR}/lib
/usr/lib
/usr/local/lib
)
INCLUDE_DIRECTORIES(common ${OPENSCENEGRAPH_INCLUDE_DIR})
LINK_DIRECTORIES(${OPENSCENEGRAPH_LIB_DIR})
add_executable(${PROJECT_NAME} "main.cpp")
target_link_libraries(${PROJECT_NAME} Qt5::Core)
My main.cpp file is
#include <QCoreApplication>
#include <osgViewer/Viewer>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
osgViewer::Viewer viewer;
// viewer.setSceneData( osgDB::readNodeFile("cessna.osg") );
// return viewer.run();
return a.exec();
}
Any folks who can help. I invite them to come forward.
There is a problem with your cmake file. You need to make sure to:
Find the necessary OSG packages (osgViewer in your case).
Link your executable to the target OSG libraries.
Just like you did it for QtCore:
find_package(Qt5Core)
find_package(OpenSceneGraph REQUIRED COMPONENTS osgViewer)
# ...
target_link_libraries(${PROJECT_NAME} Qt5::Core ${OPENSCENEGRAPH_LIBRARIES} )

Preparing Qt for Windows

I'm trying to setup Qt inside windows 8.1 using jetbrains Clion IDE but it does not show anything after compiling a simple test project. This is my main.cpp file:
#include <QApplication>
#include <QtWidgets/qpushbutton.h>
int main(int argc, char **argv)
{
QApplication *app = new QApplication(argc, argv);
QPushButton *X = new QPushButton("test");
X->show();
return app->exec();
}
This is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2)
project(QtTest)
set(CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.5.0\\5.5\\mingw492_32\\")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(SOURCE_FILES main.cpp)
add_executable(QtTest WIN32 ${SOURCE_FILES})
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Multimedia REQUIRED)
qt5_use_modules(QtTest Core Widgets Gui Multimedia)
target_link_libraries(QtTest Qt5::Widgets Qt5::Gui Qt5::Core Qt5::Multimedia)
The output:
Process finished with exit code -1073741515 (0xC0000135)
Now I want some help to solve this problem.
The explicit memory management is unnecessary. You should also never need to use explicit Qt module prefixes - if you do, the build is not configured correctly. Finally, never use the qclass.h includes, use QClass.
Your code should read as follows. When your environment is correctly configured, it should build and run without any errors.
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QPushButton button("test");
button.show();
return app.exec();
}

Unresolved external symbol. C++

When I try to compile my test app it fails.
CMakeLists.txt:
cmake_minimum_required( VERSION 2.8.8 )
project( webkit-test )
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package( Qt5Core )
find_package( Qt5Gui )
find_package( Qt5OpenGL )
find_package( Qt5Network )
find_package( Qt5WebKit )
find_package( Qt5Widgets )
add_executable( webkit-test main.cpp )
qt5_use_modules( webkit-test Core Gui OpenGL Network WebKit Widgets )
C++ code:
#include <QtWidgets/QApplication>
#include <QtWebKit/QWebView>
int main( int argc, char *argv[] )
{
QString file;
if ( argc >= 2 )
file = argv[1];
QApplication a( argc, argv );
return a.exec();
}
I generate makefile by cmake -G "NMake Makefiles" (3) and then use nmake (4).
After I received that I used dumpbin /EXPORTS QtWebKit5.dll > QtWebKit5.dll.exports.txt and dumpbin /EXPORTS QtWebKit5.lib > QtWebKit5.lib.exports.txt for seeing to exporting symbols: (5) and (6).
By using Ctrl+F you can find in these files "unresolved" external symbols:
?staticMetaObject#QWebPage##2UQMetaObject##B (public: static struct QMetaObject const QWebPage::staticMetaObject)
?staticMetaObject#QWebView##2UQMetaObject##B (public: static struct QMetaObject const QWebView::staticMetaObject)
If symbols are in QtWebKit5.lib, why I have these errors when linking?
some reasons affect to this,
if you use Qt with version greater than 4.7, add this namespace to your project's ".pro" file:
QT += webkitwidgets
and if less equals than 4.7:
QT += webkit
and change your QWebView library to (if gets a warning about "No such file or directory" do not worry!)
#include <QwebView>
I added add_definitions(-DQT_DLL) to my CMakeLists.txt and now it's compiled.