I have been using QGraphicsScene and QGraphicsVideoItem as my canvas. And to control them, I've chosen to use qml and QQuickWidget to develop custom objects easily for a different module. However, I quickly ran into an issue where the QGraphicsVideoItem would not render in the QGraphicsScene but rather inside the QQuickWidget (both when the widget is empty or with a qml source attached). And the issue seems to be reproducible using a fresh project as well just by placing an empty QQuickWidget(through qt designer) anywhere inside the main ui.
Here is the reproducible code:
#include "QtGuiApplication1.h"
QtGuiApplication1::QtGuiApplication1(QWidget *parent): QMainWindow(parent)
{
ui.setupUi(this);
QGraphicsView* view = new QGraphicsView(ui.widget);
QGraphicsScene* scene = new QGraphicsScene();
QGraphicsVideoItem* video = new QGraphicsVideoItem();
QMediaPlayer* player = new QMediaPlayer();
QUrl path = QUrl::fromLocalFile("D:/My Documents/Videos/XIII.mp4");
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(view);
ui.widget->setLayout(layout);
video->setFlags(QGraphicsVideoItem::ItemIsMovable | QGraphicsVideoItem::ItemIsFocusable | QGraphicsVideoItem::ItemIsSelectable);
video->setPos(100, 100);
//view->setSceneRect(QRectF(QPointF(100, 100), QPointF(800, 600)));
view->setScene(scene);
player->setMedia(path);
player->setVideoOutput(video);
scene->addItem(video);
player->play();
view->show();
}
#pragma once
#include "ui_QtGuiApplication1.h"
#include <QtCore>
#include <QDebug>
#include <QGraphicsVideoItem>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QMediaPlayer>
#include <QUrl>
#include <QString>
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
private:
Ui::QtGuiApplication1Class ui;
};
The issue immediately went away when I removed the widget from the ui file as well. So am I missing something here?
When you call ui.widget->setLayout(layout); you break the layout that was set in Qt Designer.
Instead of programmatically creating the QGraphicsView and QVBoxLayout in your *.cpp file, add them all in Qt Designer.
(If the issue still persists, please edit your original post and include your *.ui file)
Related
I'm completely new with QT and find it quite confusing.
I created a QListView (called "listview") and would like to show that in my QMessageBox:
const int resultInfo = QMessageBox::information(this, tr("Generate Software"),
tr("The following files will be changed by the program:"),
=> Here the QListView should show up!
QMessageBox::Yes | QMessageBox::No);
if (resultInfo != QMessageBox::Yes) {
return;
}
Is that possible somehow?
QMessageBox is designed to serve texts and buttons only. See link.
If you just want to have a "More details" text, try using detail text property. In that case, you will have to create the message box using its constructor and set the icons, texts explicitly, not using the convenient information() function.
If you still want to have a list view in the message box, you should consider using QDialog, which is the base class of QMessageBox. Small example below:
#include "mainwindow.h"
#include <QDialog>
#include <QListView>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QDialog *dialog = new QDialog{this};
dialog->setWindowTitle(tr("Fancy title"));
auto button = new QPushButton{tr("OK"), this};
connect(button, &QPushButton::clicked, dialog, &QDialog::accept);
QVBoxLayout *layout = new QVBoxLayout{dialog};
layout->addWidget(new QLabel{tr("Description"), this});
layout->addWidget(new QListView{this});
layout->addWidget(button);
dialog->show();
}
MainWindow::~MainWindow()
{
}
I want to display the content I wrote in the QLineEdit widget after clicking on the QPushButton with the function ShowMessage(). How can I access that content outside of the constructor?
Tried putting the QLineEdit object I created in to a private variable.
My CPP file
#include "manualwidget.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QMessageBox>
ManualWidget::ManualWidget(QWidget *parent) : QWidget(parent)
{
QLabel *label = new QLabel(this);
QLineEdit *lineEdit = new QLineEdit(this);
QPushButton *pushButton = new QPushButton(this);
QHBoxLayout *layout = new QHBoxLayout();
label->setText("Enter text:");
pushButton->setText("Ok");
layout->addWidget(label);
layout->addWidget(lineEdit);
layout->addWidget(pushButton);
setLayout(layout);
connect(pushButton,SIGNAL(clicked()),this ,SLOT(showMessage()));
connect(lineEdit, SIGNAL(returnPressed()),this, SLOT(showMessage()));
}
void ManualWidget::showMessage(){
QMessageBox::information(this, "Message", "The text entered in the "
"manual widget window is:\n" + m_lineEdit->text());
}
My header file
#ifndef MANUALWIDGET_H
#define MANUALWIDGET_H
#include <QWidget>
#include <QLineEdit>
class ManualWidget : public QWidget
{
Q_OBJECT
public:
explicit ManualWidget(QWidget *parent = nullptr);
signals:
public slots:
private slots:
void showMessage();
private:
QLineEdit m_lineEdit;
};
#endif // MANUALWIDGET_H
#eyllanesc suggestion might work, but it should not be a prefered approach. Qt has its' own memory model, and usage of it should be prefered. Thus, "QLineEdit m_lineEdit" should be changed to e.g. "QLineEdit* m_lineEdit", and in constructor you should initialize it in the following way:
// Instance of the QLineEdit will be owned by the ManualWidget which is part of Qt memory management now.
m_lineEdit = new QLineEdit(this);
Then, the following line:
layout->addWidget(lineEdit);
Can be changed to:
layout->addWidget(m_lineEdit);
Why is it bad to use "QLineEdit m_lineEdit"? Because Qt might want to destroy this object for some reason (you still can call m_lineEdit.deleteLater()) and you might end up in the "double destruction" situation which would result in app being crashed. You can say that in this way conflicting memory models would interact.
I am trying to understand the basics of Qt. After going through some posts, I came to know that ui_mainwindow.h gets created by UIC tool and that ui_mainwindow.h contains the information about my form/ui that I created.
In my GUI, I have taken a pushbutton and a graphics view. I want a simple image (which I am creating inside the program itself) gets displayed in the graphicsView. I am trying to do it with two ways (for learning purpose):
I can write the code inside on_pushButton_clicked()(i.e. the slot of my push_button).
I am trying to put the image from the main()
Problem: I am done with the first method. I used the following lines of code inside on_pushButton_clicked() and it worked.
void MainWindow::on_pushButton_clicked()
{
//Display image in the graphics viewer
Mat img(200,200, CV_8UC3, Scalar(255,0,0));
QImage image( img.data, img.cols, img.rows, img.step, QImage::Format_RGB888 );
QGraphicsScene* scene = new QGraphicsScene();
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene->addItem(item);
ui->graphicsView->setScene(scene);
}
Now, I want to do the similar thing from the main(). To do that, now my main() looks like following:
#include "mainwindow.h"
#include <QApplication>
//For image
#include <QImage>
#include <QPixmap>
#include <QGraphicsPixmapItem>
//#include "ui_mainwindow.h"
//OPENCV Headers
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
//Display image in the graphics viewer
Mat img(200,200, CV_8UC3, Scalar(255,0,0));
QImage image( img.data, img.cols, img.rows, img.step, QImage::Format_RGB888 );
QGraphicsScene* scene = new QGraphicsScene();
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene->addItem(item);
w.ui->graphicsView->setScene(scene);
w.show();
return a.exec();
}
The above code written inside the main() works if I put #include "ui_mainwindow.h" in main.cpp. But if I comment #include "ui_mainwindow.h" and w.ui->graphicsView->setScene(scene); then, it throws error for the QGraphicsScene* scene = new QGraphicsScene();.
Error is main.cpp:32: error: allocation of incomplete type 'QGraphicsScene' QGraphicsScene* scene = new QGraphicsScene();
QUESTIONS: Why is the connection between QGraphicsScene and "ui_mainwindow.h". I understand that I need "ui_mainwindow.h" for the line w.ui->graphicsView->setScene(scene); becasue there I am using my ui but I don't understand the need for QGraphicsScene.
If You want to draw only static image better use just QLabel and set in some image
QImage *image = new QImage (":/prefix/image/vl.jpg" );
QLabel *magelab = new QLabel();
magelab->setPixmap(QPixmap::fromImage(*image));
Generaly QGraphicsScene better using with connection QGraphicsView. That is mean QGraphicsView set some object (scene) from QGraphicsScene just like:
class SomeObject : public QGraphicsView
{
Q_OBJECT
public:
explicit Schemat(QWidget *parent = 0);
private:
QGraphicsScene *scene;
};
and source
Schemat::Schemat( QWidget *parent) : QGraphicsView(parent)
{
scene = new QGraphicsScene(this);
this->setScene(scene);
// to Your scene You can add some image for example
scene->addPixmap(SOME_PIXMAP)
}
Then You create main window and add with Your QGraphicsView, for example as some part QGroupBox
void MainWindow::SetupSchemat()
{
schema = new Schemat();
QGroupBox *schbox;
QHBoxLayout *hschbox;
schbox = new QGroupBox(this);
hschbox = new QHBoxLayout(this);
schbox->setTitle("SomeScene");
hschbox->addWidget(schema); //add scene to layout
schbox->setLayout(hschbox);
}
QGraphicsScene is like some part of Your MainWindow on which you can make some animation, You can something draw. QGraphicsScene is more better to using if You want use animation not only static image it supplies more option to manipulate image (ex scaling, catch mouse click, manage via cordinates others object), and other object each should be animate or just display. To QGraphicsScene You can add some QGraphicsItem in turn each QGraphicsItem can moving onto QGraphicsScene with particular conditions defined erly or in flow. Together QGraphicsView, QGraphicsScene and QGraphicsItem can created animation or just image in some part Your main window.
Also nice explained You will find here
https://www.youtube.com/watch?v=fmSs2mNGh9I
The following code is based on the documentation of Graphics View Framework. I embed a QLineEdit in a QGraphicsScene and run the program. When I right click the line edit in the scene I get a clipped context menu. The context menu of a QGraphicsProxyWidget is drawn by the scene as a child QGraphicsProxyWidget so it get's clipped if the window is too small. I want all embedded widgets to show their context menus as top-level windows like they do when not being embedded in a QGraphicsScene. I have tried the BypassGraphicsProxyWidget flag in two ways but it doesn't work as I want. Tested on Qt 4.8 / 5.0 on Linux and Windows. Same issue on all platforms.
How can I make the embedded widgets display normal, top-level context menus with native look? Overloading QGraphicsView's contextMenuEvent gives a native top-level context menu - could I do some sort of delegation and make QGraphicsView display the context menu of embedded widgets in the scene?
#include <QApplication>
#include <QLineEdit>
#include <QGraphicsScene>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGraphicsScene scene;
QGraphicsProxyWidget *proxy = scene.addWidget(new QLineEdit(), Qt::BypassGraphicsProxyWidget);
QGraphicsView view(&scene);
view.setWindowFlags(Qt::BypassGraphicsProxyWidget);
view.show();
return app.exec();
}
Unfortunately, this is a known bug QTBUG-10683. A workaround is suggested in the last comment to the bug report.
You get native context menus by adding a QWidget that has the Qt::BypassGraphicsProxyWidget set. Children will render it's context menus as pop-ups native style.
#ifndef QGLPARENT_H
#define QGLPARENT_H
#include <QGLWidget>
#include <QGraphicsScene>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
class QGLParent : public QGraphicsView
{
private:
QGraphicsProxyWidget *child;
public:
QGLParent(QWidget *parent, QWidget *child) : QGraphicsView(parent)
{
setFrameShape(QFrame::NoFrame);
QGLFormat format(QGL::SampleBuffers);
format.setSwapInterval(1);
setScene(new QGraphicsScene());
setViewport(new QGLWidget(format));
//setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
child->setWindowFlags(Qt::BypassGraphicsProxyWidget);
this->child = scene()->addWidget(child);
}
protected:
void resizeEvent(QResizeEvent *event)
{
scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
child->resize(event->size().width(), event->size().height());
QGraphicsView::resizeEvent(event);
}
};
#endif
I create sample application in that I was used first two Qwidget from UI form and third widget is custom one. I created one cpp file and header file. there is no issues when build while run the application the first two widget come fine and when i click the to navigate third one, it say's error( login.exe file has stop working)
My header file is:
#ifndef LISTWIDGET_H
#define LISTWIDGET_H
#include <QObject>
#include <QWidget>
#include <QtGui>
#include <QPushButton>
class listWidget : public QWidget
{
Q_OBJECT
public:
explicit listWidget(QWidget *parent=0);
~listWidget();
public:
QPushButton *button;
signals:
};
#endif // LISTWIDGET_H
and my cpp file is:
#include "listwidget.h"
#include <QHBoxLayout>
#include <QObject>
#include <QWidget>
#include <QtGui>
listWidget::listWidget(QWidget *parent):QWidget(parent)
{
resize(100,100);
button = new QPushButton("Click here to go back");
QHBoxLayout *hLayout;
hLayout->addWidget(button);
setLayout(hLayout);
}
listWidget::~listWidget()
{
}
Here is your problem:
QHBoxLayout *hLayout;
hLayout->addWidget(button);
You forgot to either:
instantiate and assign on object for hLayout to point to:
hLayout = new QHBoxLayout();
or instantiate hLayout on the spot:
QHBoxLayout hLayout;
hLayout.addWidget(button);
Basically you are dereferencing an uninitialized pointer and in most cases your application would crash.