When I used vtk in qt creator firstly, I came into this trouble. And I cannot solve it now, asking for some help.
I followed this website https://csuzhangxc.gitbooks.io/vtk-simple-tutorial/content/getting_started/the_first.html but I got this:
error: /usr/local/lib//libvtkRenderingOpenGL2-8.2.a(vtkXRenderWindowInteractor.cxx.o): undefined reference to symbol 'XGetWindowAttributes'
/usr/lib/x86_64-linux-gnu/libX11.so.6:-1: error: error adding symbols: DSO missing from command line
Somebody know what to do?
Some file of the project are following:
.pro:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
INCLUDEPATH += /usr/local/include/vtk-8.2/
LIBS += -L/usr/local/lib/ \
-lvtkGUISupportQt-8.2 \
-lvtkIOImage-8.2 \
-lvtkInteractionImage-8.2 \
-lvtkRenderingCore-8.2 \
-lvtkCommonExecutionModel-8.2 \
-lvtkCommonCore-8.2 \
-lvtkRenderingOpenGL2-8.2 \
-lvtkInteractionStyle-8.2 \
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp:
#include "mainwindow.h"
#include <QApplication>
#include<vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
#include "vtkImageViewer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkPNGReader.h"
#include "QVTKWidget.h"
#include "vtkImageData.h"
#include "vtkActor.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QVTKWidget widget;
char filename[] = "/home/bichongke/Downloads/dinosaur.png";
vtkPNGReader* reader =vtkPNGReader::New();
reader->SetFileName(filename);
reader->Update();
vtkImageViewer* imageView = vtkImageViewer::New();
imageView->SetInputConnection(reader->GetOutputPort());
widget.SetRenderWindow(imageView->GetRenderWindow());
imageView->SetupInteractor(widget.GetRenderWindow()->GetInteractor());
imageView->SetColorLevel(138.5);
imageView->SetColorWindow(233);
int* dims = reader->GetOutput()->GetDimensions();
widget.resize(dims[0],dims[1]);
widget.show();
a.exec();
imageView->Delete();
reader->Delete();
return 0;
//MainWindow w;
//w.show();
//return a.exec();
}
Related
So I've created a qrc file and have a file at /stylesheets/main.qss.
I have stylesheet information in this main.qss file. My goal here is to have a qss file I can work out of and potentially hot reload over time. My issue is that when I debug there is no /stylesheets/main.qss in the debug build location. So it loads the file as an empty string, don't even get an exception. What am I missing?
main.qss
/*#MainBackgroundColor = rgb(40,40,40)*/
/*#MainBorderColor = rgb(0,102,255)*/
/*#MainTextColor = rgb(255,255,255)*/
*
{
color: rgb(255,255,255);
background-color: rgb(40,40,40);
}
QStatusBar
{
border-top: 3px solid rgb(0,102,255);
}
Loading the stylesheet
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
//We want a frameless window.
setWindowFlags(Qt::FramelessWindowHint);
//Load the style sheet into the window
QFile File(":/stylesheets/main.qss");
File.open(QFile::ReadOnly);
QString stylesheet = QLatin1String(File.readAll());
//Setup the UI
ui->setupUi(this);
this->setStyleSheet(stylesheet);
}
MainWindow::~MainWindow()
{
delete ui;
}
resources.qrc
<RCC>
<qresource prefix="/">
<file>stylesheets/main.qss</file>
</qresource>
</RCC>
.pro file
#-------------------------------------------------
#
# Project created by QtCreator 2019-02-20T18:02:31
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = SmartDraw
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp \
stylesheetloader.cpp
HEADERS += \
mainwindow.h \
stylesheetloader.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
resources.qrc
DISTFILES += \
stylesheets/main.qss
EDIT: Found the solution. Apparently Qt isn't very good about updating everything if you happen to have the pro file open. If something super obviously wrong happens like this you need to run Build->Clean All then Build->Run QMake to get everything stituated again.
What I do is keep the stylesheet file (*.qss) next to the app while debugging. Then load it in main.cpp and subscribe for changes using QFileSystemWatcher.
This way I can edit the *.qss file with a nice editor, like SublimeText, and every time I save it, I can see the changes inmediatly:
#include "mydialog.h"
#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QTextStream>
#include <QSharedPointer>
#include <QFileSystemWatcher>
typedef QSharedPointer<QFileSystemWatcher> QWatcherPtr;
void setStyleSheet(QApplication &a, const QString &strPath, const bool &subscribe = false)
{
// set stylesheet
QFile f(strPath);
if (!f.exists())
{
qDebug() << "[ERROR] Unable to set stylesheet," << strPath << "file not found.";
}
else
{
// set stylesheet
f.open(QFile::ReadOnly | QFile::Text);
QTextStream ts(&f);
a.setStyleSheet(ts.readAll());
f.close();
// subscribe to changes (only once)
if (!subscribe)
{
return;
}
QWatcherPtr watcher = QWatcherPtr(new QFileSystemWatcher);
watcher->addPath(strPath);
QObject::connect(watcher.data(), &QFileSystemWatcher::fileChanged, &a,
[&a, watcher, strPath]()
{
setStyleSheet(a, strPath, false);
});
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// set stylesheet and subscribe to changes
setStyleSheet(a, "./style.qss", true);
MyDialog w;
w.show();
return a.exec();
}
I'd like to show and then close a dialog after 5 seconds within my main() function in a Qt Console application. Here is my code that works:
Main.cpp:
#include <QApplication>
#include "splash.h"
#include <QTimer>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// some stuff to do
Splash splash;
QTimer::singleShot(5000, &splash, SLOT(close()));
splash.exec();
// some more stuff to do
exit(0);
// return a.exec();
}
text_console.pro:
QT += widgets \
gui
CONFIG += c++11 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp \
splash.cpp
FORMS += \
splash.ui
HEADERS += \
splash.h
The above code complies and works, but I do not get complete intellisense for the splash object (e.g. setModal(), exec()). What am I doing wrong?
I am using Qt5 in linux.
Anyone got QT 5.6 working with OpenCV ?
Tried OpenCV latest 2.4.13 (at time of writing) and went back to OpenCV 2.4.9.
None worked - program will compile, link but crash on "During Startup program exited with code 0xc0000139". I never had any problems with QT 5.5.1 and OpenCV.
Here is stripped down code:
**mainwindow.cpp:**
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv/cv.h>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
cv::Mat image = cv::imread("C:/Users/foo/pics/cheetah.jpg");
cv::namedWindow("My Image");
cv::imshow("My Image", image);
cvWaitKey(0);
}
MainWindow::~MainWindow()
{
delete ui;
}
**main.cpp (used default):**
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
**mainwindow.h: (used default)**
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
**test_opencv.pro:**
INCLUDEPATH += "C:\Users\foo\Downloads\opencv\build\include"
LIBS += -LC:\Users\foo\Downloads\opencv\build\lib \
-lopencv_core249.dll \
-lopencv_highgui249.dll \
-lopencv_imgproc249.dll \
-lopencv_features2d249.dll \
-lopencv_video249.dll \
-lopencv_calib3d249.dll
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test_opencv
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
Path set:
C:\Qt\Tools\mingw492_32\bin
C:\Qt\5.6\mingw49_32\bin
C:\Users\foo\Downloads\opencv\build\bin
QT works fine with no OpenCV calls. With above code, running with QT Debug will see this:
"During Startup program exited with code 0xc0000139".
QT Error message
I have double checked my path. The OpenCV DLLs are defined in the Path. problem is not about not finding the runtime dynamically link dlls. I also checked my System32 and other directories for any old OpenCV dlls that might interfere as the path is traversed. No issues.
OpenCV was built with CMake and successfully build and installed. No issues there.
Thanks
Sean
I'm trying to build a really simple Qt program using FFMpeg library.
Currently I just want to open and close a video file.
Here is my project file:
QT += core gui
TARGET = avtest01
TEMPLATE = app
INCLUDEPATH += /usr/local/include
LIBS += -L/usr/local/lib -lavformat
SOURCES += main.cpp
And my code:
#include <QDebug>
extern "C" {
#include <libavformat/avformat.h>
}
int main(int argc, char *argv[])
{
if(argc > 1)
{
AVFormatContext *format_context;
qDebug() << argv[1];
if(avformat_open_input(&format_context, argv[1], NULL, NULL) == 0)
{
qDebug() << "open";
avformat_close_input(&format_context);
}
else
qDebug() << "error opening " << argv[1];
}
return 0;
}
Unfortunately, the linker fails:
Undefined symbols for architecture x86_64:
"avformat_open_input(AVFormatContext**, char const*, AVInputFormat*, AVDictionary**)", referenced from:
_main in main.o
"avformat_close_input(AVFormatContext**)", referenced from:
_main in main.o
I'm using Qt 5.1.0 on MacOS.
Your code worked for me after I added av_register_all(); to main.
My guess is that you have avformat compiled for 32 bit. You can confirm by running file /usr/local/lib/libavformat.dylib in terminal.
The output should look like:
/usr/local/lib/libavformat.dylib: Mach-O 64-bit dynamically linked shared library x86_64
Until now I was working with OpenCV in VS 2012. Everything worked. Now I'm trying to work in QtCreator but I have a problem. When I run the project in debug I get the error:
"C:\Qt\Qt5.0.2\Tools\QtCreator\bin\DetectorPietoni\mainwindow.cpp:4: error: C1083: Cannot open include file: 'opencv2/core/core.hpp': No such file or directory"
In release mode I get the error:
"mainwindow.obj:-1: error: LNK2019: unresolved external symbol "class cv::Mat __cdecl cv::imread(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?imread#cv##YA?AVMat#1#AEBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z) referenced in function "private: void __cdecl MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked#MainWindow##AEAAXXZ)".
The code I am running is the next:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
cv::Mat img = cv::imread("C:\\PedestrianDetectionDataset\\test\\pos\\1.png");
}
And my setting are:
TARGET = DetectorPietoni
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += C:\OpenCV-2.4.5\\opencv\build\include
CONFIG(release,debug|release)
{
LIBS += C:\\OpenCV-2.4.5\\opencv\\build\\x64\\vc11\\lib\\opencv_core245.lib \
C:\\OpenCV-2.4.5\\opencv\\build\\x64\\vc11\\lib\\opencv_features2d245.lib \
C:\\OpenCV-2.4.5\\opencv\\build\\x64\\vc11\\lib\\opencv_highgui245.lib \
C:\\OpenCV-2.4.5\\opencv\\build\\x64\\vc11\\lib\\opencv_imgproc245.lib \
C:\\OpenCV-2.4.5\\opencv\\build\\x64\\vc11\\lib\\opencv_ml245.lib
}
CONFIG(debug,debug|release)
{
LIBS += C:\\OpenCV-2.4.5\\opencv\\build\\x64\\vc11\\lib\\opencv_core245d.lib \
C:\\OpenCV-2.4.5\\opencv\\build\\x64\\vc11\\lib\\opencv_features2d245d.lib \
C:\\OpenCV-2.4.5\\opencv\\build\\x64\\vc11\\lib\\opencv_highgui245d.lib \
C:\\OpenCV-2.4.5\\opencv\\build\\x64\\vc11\\lib\\opencv_imgproc245d.lib \
C:\\OpenCV-2.4.5\\opencv\\build\\x64\\vc11\\lib\\opencv_ml245d.lib
}
Any ideeas?
Try coping opencv.hpp from C:\OpenCV-2.4.5\opencv\build\include\opencv2 to C:\OpenCV-2.4.5\opencv\build\include
Then it might be able to locate the include.