Error in Qt program - c++

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.

Related

Load images in QLabel

How to show more number(folder) of images in Qlabel or QScrollArea?
QImage image("E:/Raul/Images");
ui.label->setPixmap(QPixmap::fromImage(image));
Like this but i want more number images will load in one label.
Result:
Code:
#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QPointer>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWizardPage>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget widget;
QVBoxLayout *layout=new QVBoxLayout();
QLabel *label=new QLabel("<img src=:/0_0.jpg align=middle><img src=:/0_1.jpg align=middle><strong>Hello</strong> "
"<font color=red>Sai Raul!");
layout->addWidget(label);
widget.setLayout(layout);
widget.show();
return a.exec();
//
}
QLabel is not a web browser, therefore hyperlinks like <img src=/media/cc0-images/grapefruit-slice-332-332.jpg/> doesn't work, but why images from resources can not do it))).

Qt QScrollArea leftover graphic artifacts when scrolling with TranslucentBackground flag

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.

QDialog expanding base on the QLabel content

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(&notify);
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, &notify, 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(&notify);
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, &notify, 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(&notify);
//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, &notify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}

Why does QtGUI not include all GUI elements in Qt 5

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>.

Cannot play video in Qt

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"));