I work on Qt GUI C++ project. I try to display image from resource files, and there are more requirements like playing slideshow photos.
I meet difficult when debugging, it just showed white screen. I thought the image was not loaded to GraphicsView. In in main.cpp and mainwindow.ui, I keep them like default, no editing.
So please help me to fix this problem. Thanks in advance
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>
#include <QGraphicsPixmapItem>
#include <QMessageBox>
#include <QGraphicsView>
#include <QStackedWidget>
#include <QGraphicsScene>
#include <QDataStream>
#include <QFile>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap file(":/rose.jpg");
QStackedWidget *temp = new QStackedWidget();
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem img(file);
scene.addItem(&img);
temp->addWidget(&view);
setCentralWidget(view);
}
MainWindow::~MainWindow()
{
delete ui;
}
Your QGraphicsView and QGraphicsScene instance are local variable, they will be destroyed when out of the MainWindow constructor scope.
please try it like this.
QGraphicsScene *scene = new QGraphicsScene(this);
QGraphicsView *view = new QGraphicsView(scene);
If it still does't work, please check your QPixmap file is null or not just like what #Wagmare say.
Related
I'm a beginner in Qt, currently reading this : https://zetcode.com/gui/qt5/menusandtoolbars/
When I declare QActions in a QToolBar, the QPixmap objects (turned into QIcons) are not showing :
No icons, only text
However, the QPixmap images are actually showing when I declare a QMenu without the toolbar.
I am using Qt6 ; working on Fedora ; no warning shown on my compiler.
simple_menu.hpp
#ifndef SIMPLE_MENU_HPP
#define SIMPLE_MENU_HPP
#include <QMainWindow>
#include <QApplication>
class SimpleMenu : public QMainWindow
{
public:
SimpleMenu(QWidget *parent = nullptr);
};
#endif
simple_menu.cpp
#include "simple_menu.hpp"
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QIcon>
#include <QAction>
SimpleMenu::SimpleMenu(QWidget *parent)
: QMainWindow(parent)
{
QPixmap newpix("new.png");
QPixmap openpix("open.png");
QToolBar *toolbar = addToolBar("main toolbar");
toolbar->addAction(QIcon(newpix), "New File");
toolbar->addAction(QIcon(openpix), "Open File");
}
main.cpp
#include "simple_menu.hpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SimpleMenu window;
window.resize(350, 250);
window.setWindowTitle("Tuto Toolbar");
window.show();
return app.exec();
}
Maybe the system cannot find your pictures. Please try to put the full file path (like :).
QPixmap newpix("c:\mydoc\pictures\new.png");
To check if the pictures are loaded, you can do something a bit different :
QPixmap newpix;
qDebug() << newpix.load("c:\mydoc\pictures\new.png");
the load method returns true or false in case of loading fail.
Anyway, if you want to embed your icons in the final EXE file, check the Qt resource system
It's a convenient system to achieve what you want to do, and to avoid dealing with storage paths on your local drives.
Good luck !
A little at lost as to why QChartView will expand when put inside of a QTabWidget.
Here's a picture of the application when QChartView is not expanding (because it's hidden).
The black portion of the app is QOpenGLWidget.
When I click on the chart view, it will gradually increase in size until QOpenGLWidget is hidden.
When QChartView is just in a QVBoxLayout with QOpenGLWidget, then this effect does not occur. It's only when I add QChartView inside of the QTabWidget that this happens. I'm trying to figure out how to have QChartView not expand, and resize the same way other widgets do (such as the QTextEdit widget in this example).
Here's the code, which was written as a minimal example to reproduce the effect.
#include <QApplication>
#include <QChart>
#include <QChartView>
#include <QMainWindow>
#include <QOpenGLWidget>
#include <QTabWidget>
#include <QTextEdit>
#include <QVBoxLayout>
int
main(int argc, char** argv)
{
QApplication app(argc, argv);
// Main Window
QMainWindow main_window;
main_window.resize(1280, 720);
main_window.show();
// Central Widget
QWidget central_widget(&main_window);
main_window.setCentralWidget(¢ral_widget);
QVBoxLayout layout(¢ral_widget);
central_widget.setLayout(&layout);
// OpenGL Widget
QOpenGLWidget gl_widget(¢ral_widget);
gl_widget.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
layout.addWidget(&gl_widget);
// Tab Widget
QTabWidget tab_widget(¢ral_widget);
layout.addWidget(&tab_widget);
// Log
QTextEdit text_edit(&tab_widget);
text_edit.setReadOnly(true);
tab_widget.addTab(&text_edit, "Log");
// Chart View
QtCharts::QChartView chart_view(&tab_widget);
tab_widget.addTab(&chart_view, "Chart");
return app.exec();
}
The problem is caused because the QChartView has the expansion sizePolicy as opposed to the QOpenGLWidget, so when it becomes visible it expands, hiding the other widget. The solution is to set a stretch factor associated with each widget in the layout:
layout.addWidget(&gl_widget, 1);
layout.addWidget(&tab_widget, 1);
What I want to do now is simply show my .qrc picture to my scene, but something keep going wrong with my program. I've checked my path and it should be fine.
thanks in advance for the great help!
my picture is placed in a folder called "img" under my project.(img folder was newed by right clicking main.cpp and choose "show containing folder")
it shows [qrc_myresources.cpp] Error 1 as i run it, which quite confuse me. I've searched for stack overflow but didn't find the solution.
compile output:
00:30:13: Running steps for project shoot...
00:30:13: Configuration unchanged, skipping qmake step.
00:30:13: Starting: "/usr/bin/make"
/home/pd2vm/Qt5.9.2/5.9.2/gcc_64/bin/rcc -name myresources ../shoot/myresources.qrc -o qrc_myresources.cpp
RCC Parse Error: '../shoot/myresources.qrc' Line: 10 Column: 6 [expected tag]
Makefile:597: recipe for target 'qrc_myresources.cpp' failed
make: *** [qrc_myresources.cpp] Error 1
00:30:13: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project shoot (kit: Desktop Qt 5.9.2 GCC 64bit)
When executing step "Make"
00:30:13: Elapsed time: 00:00.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QTimer>
#include <QKeyEvent>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
virtual void keyPressEvent(QKeyEvent *e);
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
QGraphicsPixmapItem *player;
QTimer *timer;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
timer(new QTimer)
{
QGraphicsScene * scene = new QGraphicsScene(0,0,1200,880);
ui->setupUi(this);
ui->graphicsView->setScene(scene);
//player
player = new QGraphicsPixmapItem(QPixmap(":/img/whitedog.png"));
scene->addItem(player);
player->setPos(600, 880);
timer->start(10);
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
myresources.qrc
<RCC>
<qresource prefix="/">
<file>img/whitedog.png</file>
</qresource>
<RCC/>
I don't know much about Qt but that looks like xml and so shouldn't the slash go before the closing tag name. Like this:
<RCC>
...
</RCC>
Sorry if that's not the solution. Just the first thing I noticed.
My animation isn't working on this QPushButton quite the way I'm expecting.
Here's my mainwindow.cpp, as you can see nothing particularly weird here. On runtime the button appears as one might expect. I didn't have any particular movement in mind. I just wanted to see if I had set everything up correctly. As things are right now the button doesn't grow or move. What's strange here is that if I comment out the setShape.start() it defaults back to the size designated in the UI file.
My only guess thus far is that my MainWindow object isn't being displayed until after its constructor (containing the animation) has run its course. If this is the case, I'm wondering what I can do to get around it.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QPushButton>
#include <QPropertyAnimation>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPropertyAnimation setShape(ui->pushButton, "geometry");
setShape.setDuration(1000);
setShape.setStartValue(QRect(500,300,500,500));
setShape.setEndValue(QRect(800,400,500,500));
setShape.start();
}
MainWindow::~MainWindow()
{
delete ui;
}
Here's my main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QPropertyAnimation>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
This is about as basic an animation as one could create, so I'm hoping that either my intuition on this is correct, or I'm making a silly mistake. Either way any help would be greatly appreciated.
A local variable is deleted after it finishes executing its scope, and that is what happens with setShape, it is eliminated when the constructor finishes executing, what you have to do is create a pointer so that it is maintained and establish the policy of destruction to DeleteWhenStopped:
QPropertyAnimation *setShape = new QPropertyAnimation(ui->pushButton, "geometry");
setShape->setDuration(1000);
setShape->setStartValue(QRect(500,300,500,500));
setShape->setEndValue(QRect(800,400,500,500));
setShape->start(QPropertyAnimation::DeleteWhenStopped);
I've created a simple Qt Widgets application using Qt Creator on my Windows 10 machine. I use the ui file and the designer to change properties of my QMainWindow, but somehow the width, height and windowTitle properties have no effect when I set them in the designer. Example:
However, the resulting application looks like this:
Both size and windowTitle are seemingly ignored. I've also tried setting properties from code, like this (but to no avail):
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle("That's some title you've got there");
}
I have also added a layout to the centralWidget but that only had an effect on the child controls, not the actual window itself (and that seems logical).
Normally, it should work.
Can you try if the following minimal example works for you?
#include <QApplication>
#include <QMainWindow>
main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow m;
m.setWindowTitle("TEST");
m.show ();
a.exec();
}