I watched VoidRealm tutorial, and he easily include QtGui and start using it! but i do the same thing and it doesnt work for me! for example my code doesnt know the QWidget until i include QLabel! or all other Gui element...
#include <QApplication>
#include <QtGui>
#include <QtCore>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *win = new QWidget;
win->setWindowTitle("MBS");
QGridLayout *gLay = new QGridLayout;
QLabel *label1 = new QLabel("Name: ");
win->show();
return a.exec();
}
In Qt5 most of the previously known as QtGui functionality now is being called QtWidgets.
So try to write #include <QtWidgets>.
Related
I get graphic artifacts when scrolling a qscrollarea that is not a seperate window and both the scroll area and the target widget's TranslucentBackground flag is set to true. The problem doesn't happen when the scrollarea is opened as a seperate window with null parent or Qt::Window flag.
Was this behavior intended or is this a bug?
If it is a bug, is
there a fix you know of?
I've tried many other window flags but all the ones that don't open a separate window have the same issue.
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QPushButton>
#include <QScrollArea>
#include <QScrollerProperties>
#include <QScroller>
#include <QTouchDevice>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *mainWindow = new QWidget;
QHBoxLayout *buttonsLayout = new QHBoxLayout;
QWidget *buttonsWidget = new QWidget();
buttonsWidget->setLayout(buttonsLayout);
buttonsWidget->setAttribute(Qt::WA_TranslucentBackground);
buttonsWidget->setAttribute(Qt::WA_NoSystemBackground);
for(int i = 0; i < 10; ++i) {
QPushButton *button = new QPushButton("Button " + QString::number(i), buttonsWidget);
buttonsLayout->addWidget(button);
}
QScrollArea *scrollArea = new QScrollArea(mainWindow);
scrollArea->setAttribute(Qt::WA_TranslucentBackground);
scrollArea->setAttribute(Qt::WA_NoSystemBackground);
scrollArea->setWidgetResizable(true);
scrollArea->setFixedHeight(100);
scrollArea->setWidget(buttonsWidget);
scrollArea->setWindowFlag(Qt::WindowStaysOnTopHint);
mainWindow->show();
scrollArea->show();
return a.exec();
}
Example artifact when, compiled with Qt5.12.1 open source version, ubuntu 16.04.
I'd like to show and then close a dialog after 5 seconds. The dialog needs to be automatically resized (horizontally and vertically) based on the content of a label. Here is my code:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(¬ify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.adjustSize();
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
It does not not expand the dialog based on the label size. Here is how it looks:
How can I fix it? (Please also let me know if there is better way of doing this.)
I am using Qt5 in Linux.
Since you have not used a QLayout the QLabel will be displayed as large as you can, a possible request is to change the size of QDialog to the recommended size of QLabel with sizeHint():
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(¬ify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.resize(lbl->sizeHint());
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
The other possible solution is to use a QLayout:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>
void notify (int intTime=1000)
{
QDialog notify;
QVBoxLayout *lay = new QVBoxLayout(¬ify);
//notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel;
lay->addWidget(lbl);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
QTimer::singleShot(intTime, ¬ify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
I am using the Urho3D engine with Qt for an application. The problem is that both Urho3D and QApplication require to be ran from main(). For now I am using it in separate processes but IPC makes it complicated.
Is there any way to solve this issue? Thanks
My platform is Urho3D 1.5, Qt 4.71 and Windows 7 x64 and VS2015 (C++)
I'm new to both c++ and Urho3D, but I've successfully achieved it.
Simple code, haven't had further test:
awidget.h:
#ifndef AWIDGET_H
#define AWIDGET_H
#include <QWidget>
#include <QPushButton>
#include <Urho3D/Engine/Application.h>
class aWidget : public QWidget
{
Q_OBJECT
public:
explicit aWidget(QWidget *parent = 0)
{
QPushButton *button = new QPushButton(this);
connect(button, SIGNAL(clicked()), this, SLOT(pressed()));
}
public slots:
void pressed()
{
Urho3D::Context* context = new Urho3D::Context();
Urho3D::Application *application = new Urho3D::Application(context);
application->Run();
}
};
#endif // AWIDGET_H
main.cpp:
#include <QApplication>
#include <awidget.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
aWidget *widget = new aWidget();
widget->show();
return app.exec();
}
By the way, I'm using Qt 5.9.0
So the answer is quite simple. Instead of running QApplication by calling
app->exec();
it is needed to manually and regularily call this from your main loop:
app->processEvents();
This will take care that all events used by Qt are processed and QApplication will respond accordingly.
Example:
#include <QApplication>
#include <awidget.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
bool shallrun = true;
aWidget *widget = new aWidget();
widget->show();
while (shallrun)
{
app->processEvents();
...
}
...
}
Given below is the code for playing a video. On running it, it says that it could not open file.
#include <QApplication>
#include <QtMultimediaWidgets/QVideoWidget>
#include <QtMultimedia/QMediaPlayer>
#include <QtMultimedia/QMediaPlaylist>
#include <QFile>
#include <QHBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *widget=new QWidget;
widget->resize(400,300);
QMediaPlayer *player=new QMediaPlayer;
QVideoWidget *vw= new QVideoWidget;
QHBoxLayout *layout=new QHBoxLayout;
layout->addWidget(vw);
widget->setLayout(layout);
player->setVideoOutput(vw);
QFile file=("1minute.mp4");
if(!file.open(QIODevice::ReadOnly))
qDebug()<<"Could not open file";
player->setMedia(QUrl::fromLocalFile("1minute.mp4"));
player->play();
widget->show();
qDebug()<<player->availableMetaData()<<player->currentMedia().canonicalUrl();
return a.exec();
}
Where am I going wrong?
Finally found the answer. Added the complete link of the video and it started playing easily.
SO the code for the video link should be:
player->setMedia(QUrl::fromLocalFile("C:/Users/Administrator/Desktop/1minute.mp4"));
This code
#include <QtWidgets/QApplication>
#include <QLabel>
int main(int argc,char *argv[])
{
QApplication app(argc,argv);
QLabel *label = new QLabel("my first app");
label->show();
return app.exec();
}
Causing an error:
QLabel: there is no such directory
I am using Qt 5.0.1 in Windows
change
#include <QLabel>
to #include <QtWidgets/QLabel>
This is where QLabel actually resides (if this is that QLabel that you want)
Put QT += widgets in your .pro file.