Intellisense for QDialog object in Qt Console application - qt

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.

Related

underfined to symbol 'XGetWindowAttributes'

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();
}

How to display stream video frames in Qt app using Gstreamer receive from UDP socket

I would like to use Qt Multimedia to display a video stream.
I have run
#server: $gst-launch-1.0 -v videotestsrc pattern=ball ! video/x-raw,width=1280,height=720 ! jpegenc ! rtpjpegpay ! udpsink name=sink host=localhost port=34400 sync=false async=false
#client: $gst-launch-1.0 udpsrc port=34400 caps="application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)JPEG, payload=(int)26" ! rtpjpegdepay ! jpegdec ! filesink location=a.mp4
Its working fine.
now I want to server command on one terminal and Qt app should play the part of client so it will play video.
I have tried one app but its not working.
main.cpp:
#include <QApplication>
#include <QMediaPlayer>
#include <QWidget>
#include <QVideoWidget>
#include <QBoxLayout>
#include <QProcess>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
QVideoWidget *videoWidget = new QVideoWidget;
QBoxLayout *layout = new QVBoxLayout;
QMediaPlayer *player = new QMediaPlayer;
QProcess *process = new QProcess;
layout->addWidget(videoWidget);
window->setLayout(layout);
window->show();
player->setVideoOutput(videoWidget);
QString program = "gst-launch-1.0";
QStringList arguments;
arguments << "udpsrc" << "port=34400" << "caps=application/x-rtp, media=(string)video,
clock-rate=(int)90000, encoding-name=(string)JPEG, payload=(int)26" << "!" << "rtpjpegdepay"
<< "!" << "jpegdec" << "!" << "filesink location=a.mp4" ;
process->setReadChannel(QProcess::StandardError);
process->start(program, arguments);
while (!process->waitForReadyRead()) {}
player->setMedia(QMediaContent(), process);
player->play();
return a.exec();
project.pro:
QT += core gui multimedia multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = video_play_file
TEMPLATE = app
SOURCES += main.cpp\
dialog.cpp
HEADERS += dialog.h
FORMS += dialog.ui
Since Qt 5.12.2, you can pass GStreamer pipelines to QMediaPlayer::setMedia() if the GStreamer backend is used. In your case the code for setMedia() should look something like this (untested):
...
player->setMedia(QUrl("gst-pipeline: udpsrc port=34400 caps=\"application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)JPEG, payload=(int)26\" ! rtpjpegdepay ! jpegdec ! videoconvert ! xvimagesink name=\"qtvideosink\""));
...
Take a look at the documentation for more information.

QT load resource files for debugging

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();
}

QNetworkAccessManager is not sending data part of POST request

When sending POST data to server, from Qt application looks everything good but data part of HTTP part were not sent. In Wireshark in POST packet is visible correct "Content-Length" value but size of whole HTTP segment is only about 226 bytes (is independent on POST data size).
I can't find reason why data part is not send. Any idea?
The application is running in console. A complete source is below.
project.pro:
QT += widgets
QT -= gui
QT += network
CONFIG += c++11
TARGET = POSTrequest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
main.cpp:
#include <QObject>
#include <QApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QNetworkRequest request(QUrl("http://www.server.com/index.php"));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
QNetworkAccessManager manager;
QNetworkReply *reply = manager.post(request, "a=aaaaaa");
QObject::connect(reply, SIGNAL(finished()), &a, SLOT(quit()));
return a.exec();
}
This works for me on both OS X and Windows:
#post-request-36549732.pro
QT = core network
CONFIG += console c++11
CONFIG -= app_bundle
TARGET = post-request-36549732
TEMPLATE = app
SOURCES += main.cpp
// main.cpp
#include <QtNetwork>
int main(int argc, char ** argv)
{
QCoreApplication a{argc, argv};
QNetworkAccessManager manager;
QByteArray post{"a="};
post.append(QByteArray{512, 'b'});
QNetworkRequest req(QUrl("http://server/test.php"));
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
// Must be a queued connection, or else the multi-threaded manager might
// win the race and signal quit before `a.exec()` starts running. In such
// case, the `quit()` is a NOP. We don't want that.
QObject::connect(&manager, &QNetworkAccessManager::finished, &a, [](QNetworkReply * reply){
qDebug() << reply->errorString();
qApp->quit();
}, Qt::QueuedConnection);
manager.post(req, post);
return a.exec();
}
You might be interpreting your Wireshark data incorrectly. Make sure you're looking at the Reassembled TCP aspect of the HTTP request. Here's how it looks for me; server is 192.168.2.1:

In Qt 5, where is the header file for my form?

I'm new to Qt and the book is for Qt 4.
When I create a dialog.ui or mainwindow.ui file, it places it under the Folder forms. I do not see any header files or anything like that. How can I reference my dialog.ui it in my main.cpp file?
Getting error ui uses undefined class Ui::MainWindow.
Thank you!
#include <QApplication>
#include <QDialog>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Ui::MainWindow ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
dialog->show();
return a.exec();
}
.pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2014-05-22T17:21:36
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled7
TEMPLATE = app
SOURCES += main.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
qt 5 implementation which is autogenerated:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
Ui::MainWindow is a private class which is accessible by the MainWindow class only. You cannot use it anywhere else (e.g. main()).
Ui::MainWindow is defined in ui_mainwindow.h, which is auto-generated when you compile your app.
To create an GUI based on mainwindow.ui, simply instantiate a MainWindow object:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow mw;
mw.show();
return a.exec();
}
Notes:
When you create a new Qt Widgets Application in Qt Creator, main.cpp is auto-
generated for you. You don't need to write it yourself.
Qt's User Interface Compiler parses mainwindow.ui and generates
ui_mainwindow.h in your build directory. It is automatically run when you build
your app in Qt Creator. You don't need to run it manually.

Resources