I've created a Graphics View object in my mainwindow.ui file and I'm trying to display an image in that. For TextBrowser objects, I was doing like this
QTextBrowser *textBrowser_Actors = this->findChild<QTextBrowser*>("textBrowser_Actors");
textBrowser_Actors->setText(QString::fromUtf8(movie.get_actors().c_str()));
Similar way, how do I set an image after finding a GraphicsView by the below method?
QGraphicsView* movie_poster = this->findChild<QGraphicsView*>("movie_poster");
I tried the following from googling a bit, but couldn't get it working so far.
QGraphicsScene* scene = new QGraphicsScene();
movie_poster->setScene(scene);
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage("movie.jpg"));
scene->addItem(item);
movie_poster->show();
Edit-1
int main(int argc, char *argv[])
{
std::vector<Movie> movie_vector; // This is where movie DB will be read to, and new movies will be added to
MainWindow w;
w.setWindow(movie_vector[0]); // calling setWindow with first movie
w.show();
return a.exec();
}
//setWindow definition
void MainWindow::setWindow(Movie &movie) {
// Next two lines gets the textBrowser object and set its value to movie title
QTextBrowser *textBrowser_Title = this->findChild<QTextBrowser*>("textBrowser_Title");
textBrowser_Title->setText(QString::fromUtf8(movie.get_title().c_str()));
// This is where I'm trying to get GraphicsView object and set an image in it.
QGraphicsView* movie_poster = this->findChild<QGraphicsView*>("movie_poster");
QGraphicsScene* scene = new QGraphicsScene();
movie_poster->setScene(scene);
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap("C:\Users\Name\Desktop\codes\Qt\MovieDB\titanic.jpg"));
scene->addItem(item);
movie_poster->show();
}
Maybe this answer help you: https://stackoverflow.com/a/7138147/6631835
this is my code:
#include "mainwindow.h"
#include <QGraphicsView>
#include <QPixmap>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
resize(800,800);
this->image=new QImage();
openImage();
}
void MainWindow::openImage(){
image->load("/home/ztftrue/Downloads/test.jpg");
QGraphicsScene* scene=new QGraphicsScene() ;
QGraphicsView* view = new QGraphicsView(scene);
QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(*image));
scene->addItem(item);
view->show();
}
MainWindow::~MainWindow()
{
}
Related
I have a problem when use QGraphicsView and QGraphicsBlurEffect in my project. When I put them together, my program does not work normally. I wrote a tiny program to reproduce this problem.
The Widget class is inherited from QGraphicsView.
Widget::Widget(QWidget *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene(this);
this->setScene(scene);
label = new QLabel;
QPixmap pixmap = QPixmap("../partly_cloudy.png").scaledToWidth(200);
label->setPixmap(pixmap);
label->setGeometry(100,100, pixmap.width(), pixmap.height());
label->setStyleSheet("border:3px;border-color: rgb(255, 100, 0); border-style:solid;");
/* ********image won't show when adding following comment code********
QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
blur->setBlurRadius(10);
label->setGraphicsEffect(blur);
******* */
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(label);
proxyWidget->setPos(10,10);
scene->addItem(proxyWidget);
}
and main.cpp is as follows.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
This is a screenshot when QGraphicsBlurEffect is not used.
However, this is a screenshot when QLabel uses setGraphicsEffect() to bind blur effect.
To solve this problem, I tried to use a QWidget to wrap QLabel. When I did this, QLabel was rendered. However, it seems to be bounded by a rectangle area.
Widget::Widget(QWidget *parent)
: QGraphicsView(parent)
{
scene = new QGraphicsScene(this);
this->setScene(scene);
/* ********/
container = new QWidget;
container->setStyleSheet("border:3px;border-color: blue; border-style:solid;");
/******** */
label = new QLabel(container);
QPixmap pixmap = QPixmap("../partly_cloudy.png").scaledToWidth(200);
label->setPixmap(pixmap);
label->setGeometry(100,100, pixmap.width(), pixmap.height());
label->setStyleSheet("border:3px;border-color: red; border-style:solid;");
QGraphicsBlurEffect *blur = new QGraphicsBlurEffect;
blur->setBlurRadius(10);
label->setGraphicsEffect(blur);
QGraphicsProxyWidget *proxyWidget = new QGraphicsProxyWidget;
proxyWidget->setWidget(container);
proxyWidget->setPos(80,80);
qDebug() << proxyWidget->boundingRect();
scene->addItem(proxyWidget);
this->setSceneRect(0,0,640,480);
}
The screenshot of the result is.
I tried to set proxyWidget position to {0,0}, and it works normally. it seems that the position of effect rectangle will not influenced by proxyWidget position.
By the way, the version of Qt is 5.14.2.
I've searched for a long time on net. But no use. Please help or try to give some ideas how to achieve this.
I'm new to Qt programming and I want to align a QLabel with a custom QGraphicsItem i created. Here's inside my custom item constructor:
QGraphicsProxyWidget* pMyProxy = new QGraphicsProxyWidget(this);
QLabel *label = new QLabel();
label->setText("Some Text");
pMyProxy->setWidget(label);
label->setAlignment(Qt::AlignLeft);
the last line seems to have no effect whether its AlignLeft, Center, or Right.
myItem is a custom rectangle, and I'd like the label to be centered inside the rectangle, as it is the first word of label is being centered at the rectangle instead of the center of the label.
Please help
When you add a widget through a proxy, the proxy is the child of the initial item so its position will be relative to it and by default the position is (0, 0), so you see that although you set an alignment does not change, what you must do to place it in the center through setPos():
#include <QApplication>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView w;
QGraphicsScene *scene = new QGraphicsScene;
QGraphicsRectItem *item = new QGraphicsRectItem;
item->setRect(QRect(0, 0, 200, 200));
item->setBrush(Qt::red);
scene->addItem(item);
QGraphicsProxyWidget *pMyProxy = new QGraphicsProxyWidget(item);
QLabel *label = new QLabel();
label->setText("Some Text");
pMyProxy->setWidget(label);
pMyProxy->setPos(item->boundingRect().center()-label->rect().center());
w.setScene(scene);
w.show();
return a.exec();
}
Assuming this is the item, you should do the following:
QGraphicsProxyWidget *pMyProxy = new QGraphicsProxyWidget(this);
QLabel *label = new QLabel();
label->setText("Some Text");
pMyProxy->setWidget(label);
pMyProxy->setPos(boundingRect().center()-label->rect().center());
If you want to access the widget regarding the item you must follow the children tree:
QGraphiscScene
└── QGraphiscItem
└── (children)
QGraphicsProxyWidget
└── (widget)
QLabel
Example:
main.cpp
#include <QApplication>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
#include <QLabel>
#include <QTimer>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsView w;
QGraphicsScene *scene = new QGraphicsScene;
QGraphicsRectItem *item = new QGraphicsRectItem;
item->setRect(QRect(0, 0, 200, 200));
item->setBrush(Qt::red);
scene->addItem(item);
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
item->setFlag(QGraphicsItem::ItemIsMovable, true);
QGraphicsProxyWidget *pMyProxy = new QGraphicsProxyWidget(item);
QLabel *label = new QLabel();
label->setText("Some Text");
pMyProxy->setWidget(label);
pMyProxy->setPos(item->boundingRect().center()-label->rect().center());
w.setScene(scene);
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&](){
if(!scene->selectedItems().isEmpty()){
auto *item = scene->selectedItems().first();
if(!item->childItems().isEmpty()){
auto proxy = static_cast<QGraphicsProxyWidget *>(item->childItems().first());
if(proxy){
auto label = qobject_cast<QLabel *>(proxy->widget());
if(label){
label->setText(QString("x: %1, y: %2").arg(item->pos().x()).arg(item->pos().y()));
}
}
}
}
});
timer.start(100);
w.show();
return a.exec();
}
everyone! I try to set a click property to a QMediaPlayer Element, but I can not find the mode to make it, and if I try to put a button in front to Video, the button puts behind to video, even with
button->raise();
videoWidget->lower();
And If I put a Button to fullscreen the screen turns in black and don't shows the video
this id the code of the video player
QMediaPlayer *player = new QMediaPlayer(this);
QVideoWidget *vw = new QVideoWidget(this);
QMediaPlaylist *PlayList = new QMediaPlaylist(this);
PlayList->addMedia(QUrl::fromLocalFile("/home/user/Videos/video.mp4"));
PlayList->setPlaybackMode(QMediaPlaylist::Loop);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(vw);
player->setVideoOutput(vw);
player->setPlaylist(PlayList);
vw->setGeometry(0,0,800,480);
vw->show();
player->play();
One possible solution is to create a widget where the QVideoWidget is placed through a layout, the button is also added and we change the position through the resizeEvent() event.
#include <QApplication>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QPushButton>
#include <QUrl>
#include <QVBoxLayout>
#include <QVideoWidget>
#include <QDebug>
class VideoWidgetButton: public QWidget{
QPushButton *btn;
QVideoWidget *vw;
QMediaPlayer *player;
public:
VideoWidgetButton(QWidget *parent=Q_NULLPTR):QWidget(parent){
setLayout(new QVBoxLayout);
layout()->setContentsMargins(0, 0, 0, 0);
vw = new QVideoWidget(this);
btn = new QPushButton(this);
btn->setIcon(QIcon(":/icons/tux.jpeg"));
btn->resize(QSize(128, 128));
btn->setIconSize(QSize(128, 128));
connect(btn, &QPushButton::clicked, [](){
qDebug()<<"clicked";
});
layout()->addWidget(vw);
player = new QMediaPlayer(this);
player->setVideoOutput(vw);
QMediaPlaylist *playList = new QMediaPlaylist(this);
playList->addMedia(QUrl("qrc:/video/SampleVideo_1280x720_1mb.mp4"));
playList->setPlaybackMode(QMediaPlaylist::Loop);
player->setPlaylist(playList);
player->play();
}
protected:
void resizeEvent(QResizeEvent *ev){
btn->move(rect().bottomRight()-btn->rect().bottomRight());
return QWidget::resizeEvent(ev);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
VideoWidgetButton w;
w.resize(640, 480);
w.show();
return a.exec();
}
The complete example can be found in the following link.
I am trying to overlay a few buttons over my video player.
I have added a new class called overlay.cpp that subclassed a QWidget for the overlay purpose.
What I did in my code is to overlay button onto the video. In my centralWidget I have added a verticalLayout and morph it into a QWidget. The video was added into this verticalLayout. Upon program is running, the video is playing well. However, what's not working is the overlay of the button. The background doesn't seem to appear transparent even though it was set. I am not sure what is causing it to not appear transparent.
My code is as follows:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[]){
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUI(this);
initializeVideo();
initializeButton();
}
MainWindow::~MainWindow(){
delete ui;
}
void MainWindow::initializeVideo(){
QVideoWidget *v_widget = new QVideoWidget;
QMediaPlayer *m_player = new QMediaPlayer;
m_player->setMedia(QUrl::fromLocalFile("C:/user/Desktop/video.wmv"));
m_player->setVideoOutput(v_widget);
ui->verticalLayout->addWidget(v_widget);
m_player->player();
v_widget->show();
}
void MainWindow::initializeButton(){
QFrame *b_frame = new QFrame;
QGridLayout *grid = new QGridLayout;
b_frame->setLayout(grid);
b_frame->setAttribute(Qt::WA_TranslucentBackground, true);
QPushButton *buttonStop = new QPushButton;
buttonStop->setText("STOP");
grid->addWidget(buttonStop, 0, 0, Qt::AlignTop);
overlay *overlay_1 = new overlay;
QGridLayout *gridLayout = new QGridLayout;
gridLayout->addWidget(b_frame);
overlay_1->setLayout(gridLayout);
overlay_1->setParent(ui->verticalWidget);
overlay_1->show();
b_frame->show();
}
overlay.cpp
#include "overlay.h"
overlay::overlay(QWidget *parent): QWidget(parent){
this->setAttribute(Qt::WA_TranslucentBackground, true);
}
Move declaration of QVideoWidget *v_widget and QMediaPlayer *m_player to mainwindow.h like this:
private:
Ui::MainWindow *ui;
QVideoWidget *v_widget;
QMediaPlayer *m_player;
In mainwindow.cpp:
void MainWindow::initializeVideo()
{
v_widget = new QVideoWidget(this);
m_player = new QMediaPlayer(this);
m_player->setMedia(QUrl::fromLocalFile("C:/user/Desktop/video.wmv"));
m_player->setVideoOutput(v_widget);
ui->verticalLayout->addWidget(v_widget);
m_player->play();
}
void MainWindow::initializeButton()
{
QGridLayout *grid = new QGridLayout(v_widget);
QPushButton *buttonStop = new QPushButton(this);
buttonStop->setText("STOP");
grid->addWidget(buttonStop, 0, 0, Qt::AlignTop);
}
This will add "STOP" button on top of QVideoWidget.
Specify widget's parent when crating it. new QVideoWidget(this) will create new QVideoWidget as child of current MainWindow widget. If you are creating child of already visible widget you do not need to call show() on it.
I have a custom widget with some standard child widgets inside. If I make a separate test project and redefine my custom widget to inherit QMainWindow, everything is fine. However, if my custom widget inherits QWidget, the window opens, but there are no child widgets inside.
This is the code:
controls.h:
#include <QtGui>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
class Controls : public QWidget
{
Q_OBJECT
public:
Controls();
private slots:
void render();
private:
QWidget *frame;
QWidget *renderFrame;
QVBoxLayout *layout;
QLineEdit *rayleigh;
QLineEdit *mie;
QLineEdit *angle;
QPushButton *renderButton;
};
controls.cpp:
#include "controls.h"
Controls::Controls()
{
frame = new QWidget;
layout = new QVBoxLayout(frame);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
frame->setLayout(layout);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
main.cpp:
#include <QApplication>
#include "controls.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Controls *controls = new Controls();
controls->show();
return app.exec();
}
This opens up a window with correct dimensions, but with no content inside.
Bear in mind this is my first day using Qt. I need to make this work without inheriting QMainWindow because later on I need to put this on a QMainWindow.
You're missing a top level layout:
Controls::Controls()
{
... (yoour code)
QVBoxLayout* topLevel = new QVBoxLayout(this);
topLevel->addWidget( frame );
}
Or, if frame is not used anywhere else, directly:
Controls::Controls()
{
layout = new QVBoxLayout(this);
rayleigh = new QLineEdit;
mie = new QLineEdit;
angle = new QLineEdit;
renderButton = new QPushButton(tr("Render"));
layout->addWidget(rayleigh);
layout->addWidget(mie);
layout->addWidget(angle);
layout->addWidget(renderButton);
setFixedSize(200, 400);
connect(renderButton, SIGNAL(clicked()), this, SLOT(render()));
}
Note that setLayout is done automatically when QLayout is created (using parent widget)
You'll want to set a layout on your Controls class for managing its child sizes. I'd recommend removing your frame widget.
controls.cpp
Controls::Controls()
{
layout = new QVBoxLayout(this);
.
.
.
}
main.cpp
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}