点击其他按钮后如何重复使用同一个窗口?

时间:2017-12-27 05:19:08

标签: qt

我正在尝试使用幻灯片放映创建图像查看器。当用户点击播放按钮时,观看者开始显示图像。当用户点击停止按钮时,查看器停止显示图像。停止后,当用户再次点击播放按钮时,观众将继续显示剩余的图像。我的问题是,当用户点击停止按钮并再次点击播放按钮时,我不知道如何重复使用在开头创建的同一窗口来显示剩余的图像。

Button3是播放按钮。

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileSystemModel>
#include "showpic.h"
#include <QBasicTimer>
#include <QTimer>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_3_clicked();
    void tick();        
    void on_pushButton_4_clicked();

private:
    Ui::MainWindow *ui;
    QFileSystemModel *model;
    QString filesPath;
    ShowPic *showpic;
    QStringList filenames;
    QStringList::const_iterator m_imageIt;
    QTimer m_timer;
};

#endif // MAINWINDOW_H

showpic.h

#ifndef SHOWPIC_H
#define SHOWPIC_H

#include <QWidget>

namespace Ui {
class ShowPic;
}

class ShowPic : public QWidget
{
    Q_OBJECT

public:
    explicit ShowPic(QWidget *parent = 0);
    ~ShowPic();

private:
    Ui::ShowPic *ui;    

public:
    void addPixmap(const QPixmap &pixmap);

};

#endif // SHOWPIC_H

的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"
#include<QFileDialog>
#include<QFileSystemModel>
#include<QStringList>
#include <QTreeView>
#include <QGraphicsScene>
#include <QTime>
#include <QDebug>
#include <iostream>
#include <QTimer>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    filenames.append("C:\\test\\image.jpg");
    filenames.append("C:\\test\\apple.jpg");
    filenames.append("C:\\test\\orange.jpg");
    filenames.append("C:\\test\\lemon.jpg");
    filenames.append("C:\\test\\grape.jpg");

    m_timer.setInterval(1000);
    connect(&m_timer, SIGNAL(timeout()), this, SLOT(tick()));   
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::tick(){
    showpic->addPixmap(*m_imageIt);
    m_imageIt ++;
    if(m_imageIt == filenames.end()){
        m_timer.stop();
        m_imageIt = filenames.begin();
    }
}

void MainWindow::on_pushButton_3_clicked()  //click on the play button
{
    if(!filenames.isEmpty()){ // initial click
        showpic = new ShowPic();
        m_timer.start();
        showpic->setWindowState(Qt::WindowMaximized);
        showpic->show();
    } else if( ) { // click on the play button again,
        m_timer.start();  //???
        showpic->setWindowState(Qt::WindowMaximized); //???
        showpic->show();  //???
    }
}

void MainWindow::on_pushButton_4_clicked() // click on the stop button
{
    m_timer.stop();
}

showpic.cpp

#include "showpic.h"
#include "ui_showpic.h"

ShowPic::ShowPic(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ShowPic)
{
    ui->setupUi(this);
    ui->graphicsView->setScene(new QGraphicsScene);
    ui->horizontalLayout->addWidget(ui->graphicsView);
    this->setLayout(ui->horizontalLayout);
}

ShowPic::~ShowPic()
{
    delete ui;
}

void ShowPic::addPixmap(const QPixmap &pixmap){

    ui->horizontalLayout->addWidget(ui->graphicsView);
    this->setLayout(ui->horizontalLayout);
    ui->graphicsView->setScene(new QGraphicsScene);
    ui->graphicsView->scene()->addPixmap(pixmap);        
    ui->graphicsView->fitInView(ui->graphicsView->scene()->itemsBoundingRect() ,Qt::KeepAspectRatio);
}

2 个答案:

答案 0 :(得分:0)

on_pushButton_3_clicked的其他部分在任何情况下都不会运行,因为MainWindow创建时会填充文件名。在您的代码中,您可以在每次点击时创建新的showpic。

首先,在MainWindow的构造函数中将showpic设置为NULL;

showpic = NULL;

并像这样更改on_pushButton_3_clicked方法;

void MainWindow::on_pushButton_3_clicked()  //click on the play button
{
    if(showpic == NULL){
        showpic = new ShowPic();
    }
    if(!showpic->isVisible()){
        showpic->setWindowState(Qt::WindowMaximized);
        showpic->show();
    }
    m_timer.start();
}

最后,我现在没有QT,所以我的回答可能包含拼写错误/语法错误。

答案 1 :(得分:0)

这对我有用。我测试了场景清理方法,它对我来说很好。但是在这里我决定只将指针存储到QGraphicsPixmapItem作为成员变量,并且只是为它设置一个新的像素图,而不是经常清除场景。对我来说这样看起来更优雅。

#include <QtWidgets>

class SlideView : public QWidget
{
    Q_OBJECT
public:
    SlideView(QWidget *parent = nullptr) : QWidget(parent)
    {
        setLayout(new QHBoxLayout);
        layout()->addWidget(&view);
        view.setScene(new QGraphicsScene(this));
        pixmap_item = new QGraphicsPixmapItem;
        view.scene()->addItem(pixmap_item);
    }
    void setPixmap(const QPixmap &pixmap)
    {
        pixmap_item->setPixmap(pixmap);
        view.fitInView(view.scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
    }
private:
    QGraphicsView view;
    QGraphicsPixmapItem *pixmap_item = nullptr;
};

class MainWidget : public QWidget
{
    Q_OBJECT
public:
    MainWidget(QWidget *parent = nullptr) : QWidget(parent)
    {
        slide_iterator = slides.begin();
        setLayout(new QHBoxLayout);
        QPushButton *play_button = new QPushButton("Play");
        QPushButton *stop_button = new QPushButton("Stop");
        layout()->addWidget(play_button);
        layout()->addWidget(stop_button);

        connect(play_button, &QPushButton::clicked, this, &MainWidget::play);
        connect(stop_button, &QPushButton::clicked, this, &MainWidget::stop);
        connect(&timer, &QTimer::timeout, this, &MainWidget::showNextSlide);
    }
public slots:
    void play() {timer.start(1000); view.showMaximized(); view.activateWindow();}
    void stop() {timer.stop();}
    void showNextSlide()
    {
        QPixmap pixmap(*slide_iterator);
        view.setPixmap(pixmap);
        slide_iterator++;
        if(slide_iterator == slides.end())
            slide_iterator = slides.begin();
    }
private:
    QTimer timer;
    QStringList slides{"one.png", "two.png", "three.png"};
    QStringList::const_iterator slide_iterator;
    SlideView view;
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWidget w;
    w.show();
    return a.exec();
}

#include "main.moc"