PDA

View Full Version : Support_for_Coding_a_Picture_Viewer_(like_Flip-Flop)



cprogcoder
9th July 2017, 13:22
Hello,
I need support regarding the following code. There are some problems which do not work properly.:(
If there is an additional need about the code I can send it.
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();
}
--------------------------------------------------------

d_stranz
9th July 2017, 22:00
We don't usually do homework here. So I won't fix your code, but I will give some advice on how to do it better.

Your code is much too complicated.

- You need only one QLabel and you simply change the pixmap it displays every five seconds.
- You need only one QTimer and one timeout slot.
- You do not need to load the pictures over and over again. Make two QPixmap member variables for your Bildbetrachter class, and load the pictures in the constructor.
- You need a member variable that can be switched to indicate which picture is being displayed (for example, "bildNummer" which can have the value 1 or 2). In the constructor, set bildNummer = 2.
- Connect the Start button clicked signal to the timeout slot. Connect the QTimer's timeout signal to the timeout slot.
- In the timeout slot, the code looks at bildNummer. If bildNummer == 1, it sets bildNummer = 2, and copies pixmap 2 into the QLabel. If bildNummer == 2, it sets it to 1 and copies pixmap 1 into the QLabel. The timer is then started.
- The Stop slot stops the timer

You should also learn about Qt layouts. You are creating your child widgets with absolute positions and sizes, which is never a good thing to do. Instead, you should put a layout into your main widget class, then place all of the child widgets inside the layout. This way, when you change the size of the main window, the child widget sizes and positions will automatically be adjusted to fit the main window.