Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Support_QT_Homework_PictureViewer



  • Hello,
    I need support regarding the following code. There are some problems which do not work properly.

    Thank you in advance,

    The Task is :
    Please prepare a program which shows for a picture presentation. It should be loaded two different pictures, which
    should be shown each for five seconds (like a flip-flop).
    After start the program this should be work automatically. The pictures could be directly given which your own path.
    Additionally the program should be able with a "end" button.

    What doenst work properly :
    I had tried to to load the two pictures and and let them show for each of 5 sec. with a Timer.
    This doesnt work directly as it should be and I have to use the QMessageBox Button each time and push it.
    I think there is a logical failure or failures in it.

    The "bildbetrachter.cpp" file :

    //die Header-Dateien einbinden
    #include "bildbetrachter.h"
    #include <QPixmap>
    #include <QMessageBox>
    #include <QFileDialog>
    #include <QDateTime>
    #include <QMouseEvent>
    #include <QLCDNumber>

    //der Konstruktor
    Bildbetrachter::Bildbetrachter()
    {
    //die Größe und den Titel setzen
    resize(380,400);
    setWindowTitle("Bildbetrachter");

    //den Timer für Bild1 erzeugen
    timerZeit = new QTimer(); //Bild1
    //das Signal timeout des Timers mit dem Slot verbinden
    QObject::connect(timerZeit, SIGNAL(timeout()), this, SLOT(timerZeitSlot())); //Bild1
    
    //den Timer für Bild2 erzeugen
    timerDatum = new QTimer(); //Bild2
    QObject::connect(timerDatum, SIGNAL(timeout()), this, SLOT(timerDatumSlot()));//Bild2
    
    bildLabel1 = new QLabel(this);
    bildLabel1->setGeometry(10, 10, 10, 10);
    
    bildLabel2 = new QLabel(this);
    bildLabel2->setGeometry(10, 10, 10, 10);
    
    //der Programmstart-Button um die Bilder jeweils für 5 Sek.anzuzeigen
    startButton = new QPushButton(this);
    startButton->setGeometry(250, 10, 100, 30);
    startButton->setText("Programmstart");
    QObject::connect(startButton, SIGNAL(clicked()), this, SLOT(timerZeitSlot()));
    
    //der Beenden-Button
    beendenButton = new QPushButton(this);
    beendenButton->setGeometry(250, 45, 100, 30);
    beendenButton->setText("Beenden");
    QObject::connect(beendenButton, SIGNAL(clicked()), this, SLOT(close()));
    

    }

    //die Methode für die Anzeige - Bild1
    void Bildbetrachter::timerZeitSlot() //Bild1
    {
    timerZeit->start(5000);
    //das Bild1 anzeigen
    zeigeBild1();

    }

    //die Methode für den Timer des Datums
    void Bildbetrachter::timerDatumSlot()
    {
    //die Methode stopDatum() aufrufen
    stopDatum();

    }

    void Bildbetrachter::zeigeBild1()
    {
    QPixmap bild1;

    if (timerZeit->isActive()==false)
        return;
    else
    {
    bild1.load("C:/Qt/Bilder/1.jpg");
    //die Größe wird dabei an die Breite 200 angepasst
    bild1 = bild1.scaledToWidth(200);
    //im Label anzeigen
    bildLabel1->setPixmap(bild1);
    //die Größe des Labels an die Größe des Bildes anpassen
    bildLabel1->resize(bild1.size());
    QMessageBox::information(this,"1","1");
    
    stopUhrzeit();
    }
    

    }

    //die Methode zeigeDatum()
    void Bildbetrachter::zeigeBild2() //Bild2
    {
    QPixmap bild2;

    if (timerDatum->isActive()==false)
    {stopDatum();
        return;
    }
    else
    {
    if (timerDatum->isActive()==true)
        {
    bild2.load("C:/Qt/Bilder/2.jpg");
    //die Größe wird dabei an die Breite 200 angepasst
    bild2 = bild2.scaledToWidth(200);
    //im Label anzeigen
    bildLabel2->setPixmap(bild2);
    //die Größe des Labels an die Größe des Bildes anpassen
    bildLabel2->resize(bild2.size());
    QMessageBox::information(this,"2","Bild2");
    
    
       }
    }
    

    }

    //die Methode stopUhrzeit()- Bild1 stoppen
    void Bildbetrachter::stopUhrzeit() //Bild1
    {
    //den Timer für Bild-1 anhalten
    timerZeit->stop(); //Bild1
    bildLabel1->clear();
    //den Timer für Bild-2 starten
    timerDatum->start(5000); //Bild2
    //Bild-2 Anzeige;
    zeigeBild2(); //Bild2 anzeigen
    }

    //die Methode stopDatum()
    void Bildbetrachter::stopDatum() //Bild2
    {
    //QMessageBox::information(this,"2","Bild2");

    //den Timer für Bild-2 anhalten
    timerDatum->stop(); //Bild2
    bildLabel2->clear();
    //den Timer für Bild-2 starten
    timerZeit->start(5000); //Bild1
    //Bild-1 Anzeige;
    zeigeBild1(); //Bild1 anzeigen
    

    }


    The Header File "bildbetrachter.h"

    #ifndef BILDBETRACHTER_H
    #define BILDBETRACHTER_H

    //die Header-Dateien einbinden
    #include <QWidget>
    #include <QLabel>
    #include <QLineEdit>
    #include <QPushButton>
    #include <QLCDNumber>
    #include <QTimer>
    #include <QMouseEvent>
    #include <QDateTime>

    //oder auch
    //#include <QtWidgets>

    //unsere eigene Klasse erbt von QWidget
    class Bildbetrachter : public QWidget
    {
    //das Makro Q_OBJECT
    Q_OBJECT
    public:
    //der Konstruktor
    Bildbetrachter();

    //die Methoden und Attribute
    private:
        void zeigeUhrzeit(); //Bild1
        void zeigeDatum();   //Bild2
    
        void stopUhrzeit();  //Bild1
        void stopDatum();    //Bild2
    
    
        bool doppelpunkt;
        //der erste Timer
        QTimer* timerZeit;   //Bild1
        //der zweite Timer
        QTimer* timerDatum;  //Bild2
    
    
    //die Slots
    public slots:
        //void oeffneBilddatei();
        //void waehleDateiAus();
          void zeigeBild1();
          void zeigeBild2();
    
    private slots:
    //die Methode für den ersten Timer
          void timerZeitSlot();
    //die Methode für den zweiten Timer
          void timerDatumSlot();
    
    
    private:
        //die Attribute für die Widgets
        QLCDNumber *lcdnumber;
        QLabel* einLabel, *bildLabel1,*bildLabel2;
        QLineEdit* eingabeZeile;
        QPushButton* oeffnenButton, *beendenButton, *startButton,*auswaehlenButton;
    

    };
    #endif

    The main.cpp file

    #include <QApplication>
    #include "bildbetrachter.h"

    int main(int argc, char argv[])
    {
    QApplication app(argc, argv);
    //das Haupt-Widget erzeugen und anzeigen
    Bildbetrachter
    betrachter = new Bildbetrachter();
    betrachter->show();

    return app.exec();
    

    }



  • Hi! Your solution looks pretty complicated. Here's some inspiration for a simpler one:

    #include <QApplication>
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QLabel>
    #include <QPushButton>
    #include <QVector>
    #include <QPixmap>
    #include <QTimer>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QWidget mainWidget;
        auto layout = new QVBoxLayout(&mainWidget);
        auto label = new QLabel(&mainWidget);
        auto button = new QPushButton("End", &mainWidget);
        label->setScaledContents(true);
        label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        layout->addWidget(label);
        layout->addWidget(button);
        mainWidget.setLayout(layout);
    
        auto const pixmaps = QVector<QPixmap>()
            << QPixmap("/home/user/pic1.jpg")
            << QPixmap("/home/user/pic2.jpg");
        int current = 0;
        label->setPixmap(pixmaps.at(current));
    
        QTimer timer;
        timer.start(5000);
    
        QObject::connect(button, &QPushButton::clicked, &mainWidget, &QWidget::close);
        QObject::connect(&timer, &QTimer::timeout, [&](){current = (current + 1) % pixmaps.size(); label->setPixmap(pixmaps.at(current));});
    
        mainWidget.showFullScreen();
    
        return a.exec();
    }
    


  • Thank you very much for your support.
    I have tried to entry your code but it gives still some failure.
    The code is actually ; but it gives the failure in project environment that "pixmap is not defined in this scope" and pixmap
    does not name a type. Could you support to clarify this also ?

    Thank you in advance,

    #include "mainwindow.h"
    #include <QApplication>
    #include <QWidget>
    #include <QVBoxLayout>
    #include <QLabel>
    #include <QPushButton>
    #include <QVector>
    #include <QPixmap>
    #include <QTimer>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QPushButton *button;
    QLabel *label;
    QVBoxLayout *layout;
    //QPixmap *pixmaps;

    QWidget mainWidget;
    layout = new QVBoxLayout(&mainWidget);
    label = new QLabel(&mainWidget);
    button = new QPushButton("End", &mainWidget);
    label->setAlignment(Qt::AlignCenter);
    label->setScaledContents(true);
    label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    layout->addWidget(label);
    layout->addWidget(button);
    mainWidget.setLayout(layout);
    
    auto const pixmaps = QVector<QPixmap>()
            << QPixmap("/home/user/pic1.jpg")
            << QPixmap("/home/user/pic2.jpg");
        int current = 0;
        label->setPixmap(pixmaps.at(current));
    
        QTimer timer;
        timer.start(5000);
    
        QObject::connect(button, &QPushButton::clicked, &mainWidget, &QWidget::close);
        QObject::connect(&timer, &QTimer::timeout, [&](){current = (current + 1) % pixmaps.size(); label->setPixmap(pixmaps.at(current));});
    
        mainWidget.showFullScreen();
    
        return a.exec();
    

    }


  • Lifetime Qt Champion

    Hi
    What Qt version + compiler are you using ?
    I tried the code from @Wieland and got no errors so what line
    exactly says "pixmap is not defined in this scope" ?

    The use of lambdas

    ( the  [&](){} syntax) 
    

    requires a c++2011 enabled compiler and if you are using say
    Qt 5.5, you might need
    CONFIG += c++11
    in the .pro file ( the project file)
    Run qmake from menu after inserting.



  • Hello,

    my version was Qt 5.0.2 (32 bit) and I added the CONFIG +=c++11 in the project file,
    Thank you very much it worked properly.

    Best Regards,


Log in to reply