I am trying to load images from a folder and view the previous and next images by clicking a button on the GUI (similar to Windows Image Viewer). The names of the images in this folder are xxxx_00.jpg to xxxx_99.jpg, so I use index++ and index-- to change the filename when clicking the buttons.
My codes work well for displaying the first image, but when I click a button to view the previous or the next images, it always shows
QPixmap::scaled: Pixmap is a null pixmap
and returns an empty image (the first image disappeared but new image didn't display).
Here's my code:
In mainwindow.cpp
void MainWindow::on_pushButton_4_clicked() //previous image
{
if (index < 1)
{
index = 99;
QLabel label;
label.setText("Go back");
label.show();
}
else
{
index--;
}
RefreshFilename();
loadimage();
}
void MainWindow::on_pushButton_5_clicked() //next image
{
if (index > 98)
{
index = 0;
QLabel label;
label.setText("ALL SET");
label.show();
}
else
{
index = index + 1;
}
RefreshFilename();
loadimage();
}
void MainWindow::loadimage()
{
// image.load(filename);
// im = image.scaled(500,500,Qt::KeepAspectRatio);
imageObject = new QImage();
imageObject->load(filename);
image = QPixmap::fromImage(*imageObject);
im = image.scaled(400,400,Qt::KeepAspectRatio);
scene = new QGraphicsScene(this);
scene->addPixmap(image);
scene->setSceneRect(image.rect());
ui->mainimage->setScene(scene);
}
I have spent 2 whole days on debugging this, but still have no idea. I am looking forward to any advice and support!
BTW The Refreshfilename Function works fine, so I did not paste it here.
Cause
Since I do not know what RefreshFilename(); does, I cannot tell exactly what the cause is.
However, I see a major flaw in your code, i.e. you create a new scene each time MainWindow::loadimage is called and this causes a memory leak.
When you provide more details I will be more specific here.
Solution
Set the scene once and add a QGraphicsPixmapItem to it, then in the loadImage update the pixmap of the item.
Keep the current number in a class attribute.
Again, I will be more specific once details are added.
Example
In any case (waiting for you to provide a MVCE), I have prepared a working example according to your task description:
#define IMAGE_COUNT 99
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_imgNum(0),
m_item(new QGraphicsPixmapItem())
{
auto *widget = new QWidget(this);
auto *layoutMain = new QVBoxLayout(widget);
auto *layoutButtons = new QHBoxLayout();
auto *btnPrev = new QPushButton(tr("Previous"), this);
auto *btnNext = new QPushButton(tr("Next"), this);
auto *view = new QGraphicsView(this);
view->setScene(new QGraphicsScene(this));
view->scene()->addItem(m_item);
layoutButtons->addStretch();
layoutButtons->addWidget(btnPrev);
layoutButtons->addWidget(btnNext);
layoutButtons->addStretch();
layoutMain->addWidget(view);
layoutMain->addLayout(layoutButtons);
setCentralWidget(widget);
resize(640, 480);
loadImage();
connect(btnPrev, &QPushButton::clicked, [this](){
if (m_imgNum > 0)
m_imgNum--;
else
m_imgNum = IMAGE_COUNT;
loadImage();
});
connect(btnNext, &QPushButton::clicked, [this](){
if (m_imgNum < IMAGE_COUNT)
m_imgNum++;
else
m_imgNum = 0;
loadImage();
});
}
void MainWindow::loadImage()
{
m_item->setPixmap(QString("images/image_%1.jpg").arg(m_imgNum, 2, 10, QChar('0')));
}
where m_imgNum, m_item and loadImage are declared in the header as:
private:
inline void loadImage();
int m_imgNum;
QGraphicsPixmapItem *m_item;
Related
What is the 'proper' way to recreate a QMovie widget when it gets resized?
class Gif : public QPushButton
{
Q_OBJECT
public:
QMovie* movie = nullptr;
QTimer *timer = new QTimer(this);
int widget_width = 0;
int widget_height = 0;
Gif(QWidget* parent = 0) : QPushButton(parent) { }
void paintEvent(QPaintEvent* p)
{
// Check if the widget has been resized, if so
// delete the QLabel/QMovie and recreate them.
if (widget_width)
{
if (widget_width != width())
{
QLabel* label = this->findChild<QLabel*>("label");
label->deleteLater();
movie->deleteLater();
movie = nullptr;
}
}
// Load the gif into the QMovie/QLabel.
if (!movie)
{
auto StyleSheet = styleSheet();
QVector<QString> matches;
QRegularExpression re(R"(image:\s*url\((.*)\);)");
QRegularExpressionMatch m = re.match(StyleSheet);
for (int i = 0; i <= m.lastCapturedIndex(); i++)
matches.append(m.captured(i));
movie = new QMovie(matches[1]);
if (!movie->isValid())
qDebug() << "failed to create the QMovie.";
QLabel* label = new QLabel(this);
label->setObjectName("label");
widget_width = width();
widget_height = height();
label->setGeometry(0, 0, widget_width, widget_height);
label->setMovie(movie);
label->show();
movie->setScaledSize(QSize().scaled(widget_width, widget_height, Qt::IgnoreAspectRatio));
movie->setSpeed(150);
movie->start();
}
// Pause for 2 seconds after getting into the
// last frame.
if (movie->currentFrameNumber() == (movie->frameCount() -1))
{
movie->stop();
connect(timer, SIGNAL(timeout()), this, SLOT(resume()));
timer->start(2000);
}
}
public slots:
void resume()
{
movie->start();
}
};
Is it 'safe' to call deleteLater() and then assign movie to nullptr? won't cause any mem leak / UB?
if (widget_width != width())
{
QLabel* label = this->findChild<QLabel*>("label");
label->deleteLater();
movie->deleteLater();
movie = nullptr;
}
Also, is there any alternative to QMovie for playing a gif in a gui? it uses a lot of CPU when the gif is medium/high size
Sorry I can not offer the answer to your main question.
With your second question, "a lot of CPU usage" when playing QMovie. I found a method to set the CacheMode of QMovie. It will help to reduce the CPU usage of QMovie playing gif.
movie.setCacheMode(QMovie.CacheMode.CacheAll)
I'm using QGraphicsView and QGraphicsScene to display an uploaded image and then show some drawing on it. I'm uploading and image like so:
void MeasuresWidget::on_openAction_triggered()
{
QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty())
{
QImage image(fileName);
if (image.isNull())
{
QMessageBox::information(this, tr("Measures Application"), tr("Cannot load %1.").arg(fileName));
return;
}
scene->clear();
scene->addPixmap(QPixmap::fromImage(image).scaledToWidth(w, Qt::SmoothTransformation));
}
}
The problem i'm facing is that if i upload an image that is smaller than the one that was uploaded before, there appears to be empty space, i.e. scene maintains the size of previous image(the bigger one) and is bigger than current one. I tried maintaining original size of scene in individual variable and using setSceneRect() in each upload action:
//in constructor
originalRect = ui->graphicsView->rect();
//in upload action
scene->setSceneRect(originalRect);
but result is that size of scene always stays the same and, if it's bigger than the actual image, cuts it. I used QLabel to display an image before and i used QLabel::setScaledContents() function and it worked fine for me. So, my question is can i achieve the same behaviour with QGraphicsScene?
Update 1:
Application behaves the way i want if i create new scene every upload action. The code now looks like:
void MeasuresWidget::on_openAction_triggered()
{
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty())
{
QImage image(fileName);
if (image.isNull())
{
QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName));
return;
}
scene->clear();
scene->addPixmap(QPixmap::fromImage(image).scaledToWidth(w, Qt::SmoothTransformation));
}
}
Is this ok? Can i achieve the behaviour i want without a need to create new scene every upload action?
You just have to resize the scene when you insert your pixmap based on its size.
If you define a new class inheriting from QGraphicsScene, you can handle it easily:
class GraphicsScene: public QGraphicsScene
{
public:
GraphicsScene(QRect const& rect, QObject* parent=nullptr): QGraphicsScene(rect, parent),
background(nullptr)
{}
QGraphicsPixmapItem *addPixmap(const QPixmap &pixmap)
{
// We already have a background. Remove it
if (background)
{
removeItem(background);
delete background;
}
background = QGraphicsScene::addPixmap(pixmap);
// Move the pixmap
background->setPos(0, 0);
// Change the scene rect based on the size of the pixmap
setSceneRect(background->boundingRect());
return background;
}
private:
QGraphicsPixmapItem* background;
};
GraphicsScene* scene = new GraphicsScene(QRect());
QGraphicsView* view = new QGraphicsView();
view->setScene(scene);
view->show();
QPixmap pix1(QSize(2000, 2000));
pix1.fill(Qt::red);
QPixmap pix2(QSize(100, 300));
pix2.fill(Qt::green);
// The scene will be 2000x2000
QTimer::singleShot(1000, [=]() { scene->addPixmap(pix1); });
// The scene will be 100x300
QTimer::singleShot(10000, [=]() { scene->addPixmap(pix2); });
I have a little problem displaying my layouts. I have an application that contains a GridLayout, HBoxLayout, and VBoxLayout.
The HBoxLayout contains: a button and line edit
The GridLayout contains: progbars (1-99)
The HBoxLayout and the GridLayout are put in a VBoxLayout
My problem is when I click, The grid layout appears on the HBoxLayout. It's like the HBoxLayout wasn't a part of the VBoxLayout. So, even when I destroy the layout and create it again, I still have the same problem.
An image is displayed below to better understand my problem.
code: window.cpp
Window::Window(QWidget *parent) :
QWidget(parent){
label = NULL;
progressbar = NULL;
workerThread = NULL;
m_counter = NULL;
layout = new QHBoxLayout(NULL);
lineEdit = new QLineEdit(NULL);
m_button = new QPushButton("click", NULL);
time= new QTimer(this);
lineEdit-> setInputMask("00");;
//lineEdit->setGeometry(400,10,160,30);
lineEdit->setPlaceholderText("N between 1 & 99");
// Create and position the button
// m_button->setGeometry(100, 10, 150, 30);
QIcon icon("/home/ca/Downloads/chauvin.png");
m_button->setIcon(icon);
m_button->setIconSize(QSize(100, 30));
m_button->setToolTip("this is a beautifull button ");
//layout = new QFormLayout;
layout->addWidget(m_button);
layout->addWidget(lineEdit);
setLayout( layout );
// showTime();
time->start(1000);
setWindowTitle(tr("Digital Clock"));
// NEW : Do the connection
connect(m_button, &QPushButton::pressed, this , &Window::slotButtonPressed);}
void Window::slotButtonPressed(){
layoutgrid = new QGridLayout(NULL);
m_button->setEnabled(true);
QString contenu = lineEdit->text();
int i_contenunumber= contenu.toInt(0,10);
m_counter= new int[i_contenunumber];
memset( m_counter, 0, i_contenunumber );
label = new QLabel*[ i_contenunumber ];
progressbar = new QProgressBar*[ i_contenunumber ];
workerThread= new mythread*[ i_contenunumber ];
int x= 0;
int y= -1;
int i=0;
m_button->setText("Checked");
for(i=0;i< i_contenunumber ;i++)
{
if (i%5==0)
{
x=0;
y++;
}
int i_Randomvalue = rand() % 500 + 100;
// label[i] = new QLabel(NULL);
// //label[i]->setGeometry(100*x*1.7, 80+(50*y), 160, 30);
// label[i]->setText("Tread" + QString::number(i_Randomvalue));
// label[i]->setVisible(true);
progressbar[i] = new QProgressBar(NULL);
progressbar[i]->setRange(0, 100);
progressbar[i]->setOrientation(Qt::Horizontal);
//progressbar[i]->setGeometry(100*x*1.7,60+(50*y),150,30);
progressbar[i]->setValue(0);
progressbar[i]->setVisible(true);
layoutgrid->addWidget(progressbar[i],y,x);
setLayout(layoutgrid);
workerThread[i] = new mythread(i_Randomvalue, i);
connect(workerThread[i], &mythread::signalemit, this, &Window::barprogress);enter code here
connect(workerThread[i], &mythread::signalFinThread, this, &Window::findethread);
workerThread[i] ->start();
x++;
m_counter[i]=0;
}
// Window::setFixedSize(1000,120+(50*y-1));
removeLayout();
layoutvbox = new QVBoxLayout(NULL);
layoutvbox->addLayout(layout);
layoutvbox->addLayout(layoutgrid);
setLayout(layoutvbox);
adjustSize();
}
void Window::removeLayout ( void )
{
QLayout* po_l_layout = QWidget::layout ();
if (po_l_layout != 0)
{
QLayoutItem *item;
while ((item = po_l_layout->takeAt(0)) != 0)
po_l_layout->removeItem (item);
delete po_l_layout;
}
}
delete layout will not work here because it will not delete all the containing widgets
Instead just create a new widget that contains the layout
m_widget = new QWidget();
QGridLayout *outerLayout = new QGridLayout(m_widget);
m_layout->addWidget(m_widget);
this->setLayout(m_layout);
to remove and rebuild just delete the widget
delete m_widget
deleting a widget deletes all sub layouts and widgets contained by the widget
Once the layout of your widget is set, you can not change it unless you remove the old layout. My suggestion is to delete the old layout
//remove all of layouts widgets here
delete layout
Then create the QVBoxLayout and call setLayout(layoutvbox)
If you wish to switch back and forth, just recreate/delete when needed.
I need to scale a QtableWidget (the behaviour should be like a zoom).
But when I reimplement the paintEvent of QtableWidget and set the scale manually, like this:
void MyTableWidget::paintEvent ( QPaintEvent * event )
{
QTableWidget::paintEvent(event);
QPainter p(viewport());
p.scale(m_scale,m_scale);
p.drawRect( 0, 0, width()-1, height()-1);
}
only the border is rescaled :
And I don't see any paintEvent on QTableWidgetItem, so how can I rescale all my table ?
Thank you in advance and have a good day.
EDIT ----------------------------------------
The behaviour may seems strange so here is some explanations:
This QT window is a child of an acrobat reader window. When I unzoom the PDF on acrobat, I resize my QT window to keep the same proportions, but I would like to scale the content of the window.
example: If I unzoom my PDF, I decrease the size of my QT window to keep the same proportions, and I want to scale the content of my QT window to this new size (decrease the display size, like an unzoom). Is it clear ? :o
But for instance the view donesn't fit the window, I have this:
And I want this:
And when I zoom on my PDF, I increase the window size and scale up the content, like this:
Thank you very much for your help and your time.
Use QGraphicsScene with your QTableWidget instead, but it is not very difficult:
QGraphicsScene *scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QTableWidget *wgt = new QTableWidget;
wgt->setColumnCount(10);
wgt->setRowCount(10);
for (int ridx = 0 ; ridx < wgt->rowCount() ; ridx++ )
{
for (int cidx = 0 ; cidx < wgt->columnCount() ; cidx++)
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(QString("%1").arg(ridx));
wgt->setItem(ridx,cidx,item);
}
}
QGraphicsProxyWidget *pr = scene->addWidget( wgt );
pr->moveBy(10,10);
Scale view with:
ui->graphicsView->scale(2,2);
Better way for zooming is zoom in out by wheel. Subclass view or use eventFilter. For example:
Header:
#ifndef MYQGRAPHICSVIEW_H
#define MYQGRAPHICSVIEW_H
#include <QGraphicsView>
class MyQGraphicsView : public QGraphicsView
{
Q_OBJECT
public:
explicit MyQGraphicsView(QWidget *parent = 0);
signals:
protected:
void wheelEvent(QWheelEvent* event);
};
#endif // MYQGRAPHICSVIEW_H
Cpp:
#include "myqgraphicsview.h"
#include <QPointF>
MyQGraphicsView::MyQGraphicsView(QWidget *parent) :
QGraphicsView(parent)
{
}
void MyQGraphicsView::wheelEvent(QWheelEvent* event) {
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
// Scale the view / do the zoom
double scaleFactor = 1.15;
if(event->delta() > 0) {
// Zoom in
scale(scaleFactor, scaleFactor);
} else {
// Zooming out
scale(1.0 / scaleFactor, 1.0 / scaleFactor);
}
// Don't call superclass handler here
// as wheel is normally used for moving scrollbars
}
Usage:
MyQGraphicsView *view = new MyQGraphicsView;
view->setScene(scene);//same scene
view->show();
Result as you want:
Note that user still able to edit data etc, functionality didn't change.
Additional example, like in the Qt books.
(same MyQGraphicsView class)
#include "myqgraphicsview.h"
#include <QGraphicsProxyWidget>//and other needed includes
//just to show that signals and slots works
//with widget which placed in graphicsview
void print(int row, int column)
{
qDebug() << row+1 << column+1;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *widget = new QWidget;//container
QVBoxLayout *lay = new QVBoxLayout;
MyQGraphicsView * view = new MyQGraphicsView;//view as part of UI
QGraphicsScene * scene = new QGraphicsScene;
QTableWidget *wgt = new QTableWidget;//table which will be added to graphicsView
QObject::connect(wgt,&QTableWidget::cellPressed,print);//connection using Qt5 style(and power)
wgt->setColumnCount(10);
wgt->setRowCount(10);
for (int ridx = 0 ; ridx < wgt->rowCount() ; ridx++ )
{
for (int cidx = 0 ; cidx < wgt->columnCount() ; cidx++)
{
QTableWidgetItem* item = new QTableWidgetItem();
item->setText(QString("%1").arg(ridx));
wgt->setItem(ridx,cidx,item);
}
}
QPushButton *butt = new QPushButton("click");
lay->addWidget(view);
lay->addWidget(butt);
widget->setLayout(lay);
QGraphicsProxyWidget *pr = scene->addWidget( wgt );
pr->moveBy(10,10);
view->setScene(scene);
widget->show();
return a.exec();
}
Result:
As you can see, user can scale table, edit data and signals and slots works. All fine.
Because of the mouseEvent, I would expect the red and blue widgets to switch places on every click. Instead the red switches to the blue and then it never switches back, why?
Frame *red = NULL;
Frame *blue = NULL;
bool isRed = true;
Frame::Frame(QWidget *parent) :
QFrame(parent)
{
}
Frame::~Frame(){
printf("deleted.\n");
fflush(0);
}
void QLayout_clear(QLayout* layout, bool deleteWidgets){
QLayoutItem* item;
QLayout* childLayout;
while ((item = layout->takeAt(0)) != NULL){
QWidget* widget = item->widget();
if (widget != NULL){
layout->removeWidget(widget);
if (deleteWidgets){
delete widget;
}
} else if (childLayout = item->layout()){
QLayout_clear(childLayout, deleteWidgets);
}
//delete item;
}
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout;
this->setLayout(layout);
red = new Frame;
red->setFixedSize(100,100);
red->setStyleSheet("background-color:red");
blue = new Frame;
blue->setFixedSize(100,100);
blue->setStyleSheet("background-color:blue");
layout->addWidget(red);
}
void Widget::mouseReleaseEvent(QMouseEvent *){
printf("clicked.\n");
fflush(0);
QVBoxLayout *layout = (QVBoxLayout *)this->layout();
if (1){ //it doesnt matter if this is 1 or 0
delete layout;
layout = new QVBoxLayout;
this->setLayout(layout);
} else {
QLayout_clear(layout, false);
}
if (isRed){
layout->addWidget(blue);
isRed = false;
} else {
layout->addWidget(red);
isRed = true;
}
}
Note: here I am using a simple QFrame for the widgets to switch, in my application the widgets are much more complicated and I cannot recreate them every time I want to swap.
I believe it is not good practice from performance point of view to delete and recreate layers each time you want to switch frames, it would be easier to add both frames to the layer and then set visible only frame you want to appear. Also, boolean isRed is not necessary as info about visibility is contained within each frame itself...
Try this:
QFrame *red = NULL;
QFrame *blue = NULL;
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
red = new QFrame(this);
red->setFixedSize(100,100);
red->setStyleSheet("background-color:red");
red->setHidden(false);
blue = new QFrame(this);
blue->setFixedSize(100,100);
blue->setStyleSheet("background-color:blue");
blue->setHidden(true);
layout->addWidget(red);
layout->addWidget(blue);
this->setLayout(layout);
}
void Widget::mouseReleaseEvent(QMouseEvent *){
printf("clicked.\n");
fflush(0);
red->setHidden(!red->isHidden());
blue->setHidden(!blue->isHidden());
}
The layout destructs its children. Because addWidget takes ownership so delete layout will delete red and blue widgets too.