PDA

View Full Version : Problems by inserting Images in a QLabel



joshy198
19th September 2012, 21:28
in Levels.h


#ifndef MYWINDOW_H
#define MYWINDOW_H

#include <QPushButton>
#include <QMessageBox>
#include <QMainWindow>
#include <QHBoxLayout>
#include "QScrollArea"
#include "QImage"
#include "QLabel"
#include "QWidget"
#include "QVBoxLayout"
#include "QGroupBox"
#include "QSpacerItem"
#include "QList"

class Levels: public QMainWindow
{
Q_OBJECT

public:
Levels(){};
~ Levels(){};
void erzeugen(Levels *level);
void setzeBilder();
private:
QVBoxLayout *ramen;
QVBoxLayout *layout;
QImage *image;
QList<QLabel *> labels;
QPushButton *back;
QWidget *central;
QGroupBox *box;
QScrollArea *area;
QSpacerItem *space;
QList<QString *> bilder;


//public slots:
};
#endif



in levels.cpp

#include "levels.h"

void Levels::erzeugen(Levels *level)
{
setzeBilder();
central=new QWidget(this);
ramen= new QVBoxLayout(central);
setCentralWidget(central);
box = new QGroupBox;
back = new QPushButton(" ? Back");
for(int i=0;i<12;i++)
{
labels << new QLabel();
}
for(int i=0;i<12;i++)
{
labels.at(i)->setBackgroundRole(QPalette::Dark);
labels.at(i)->setAutoFillBackground(true);
image = new QImage(bilder.at(i));//Muss im debug-Ordner sein!!!
labels.at(i)->setPixmap(QPixmap::fromImage(*image));
labels.at(i)->setMaximumHeight(image->height());
labels.at(i)->setMaximumWidth(image->width());
}

space = new QSpacerItem(1,10);
for(int i=0;i<12;i++)
{
layout->addWidget(labels.at(i));
}


box->setLayout(layout);
area = new QScrollArea;
area->setWidgetResizable(false);
area->setWidget(box);

}
void Levels::setzeBilder()
{
for(int i=0;i<12;i++)
{
bilder << new QString;
}
bilder.at(0)=QString::fromStdString("l01.png");
bilder.at(1)=QString::fromStdString("l02.png");
bilder.at(2)=QString::fromStdString("l03.png");
bilder.at(3)=QString::fromStdString("l04.png");
bilder.at(4)=QString::fromStdString("l05.png");
bilder.at(5)=QString::fromStdString("l06.png");
bilder.at(6)=QString::fromStdString("l07.png");
bilder.at(7)=QString::fromStdString("l08.png");
bilder.at(8)=QString::fromStdString("l09.png");
bilder.at(9)=QString::fromStdString("l10.png");
bilder.at(10)=QString::fromStdString("l11.png");
bilder.at(11)=QString::fromStdString("l12.png");

}



And i Get lots of Error-Messages ...(25) which are probably allways the same...
Is there a way to set the list at the class where this class is called?

ChrisW67
20th September 2012, 02:21
The errors (I guess) are basic C++:

bilder is a list of QString pointers. You cannot assign a QString, which is what QString::fromStdString() returns, to a pointer.
QList<T>::at() returns a const reference to an item in the list: you cannot assign a value to it. You would need to use operator[]


You probably want a QList<QString> or QStringList. You do not need to use QString::fromStdString(); you can construct a QString directly from a const char*


Is there a way to set the list at the class where this class is called?
I do not know what this means.

d_stranz
20th September 2012, 02:30
Since you don't bother to tell us what the error messages are, how are we supposed to give you any help?

I will point out at least one other serious error in your code: The constructor for your "Levels" calss is wrong and should be:



Levels::Levels( QWidget * parent )
: QMainWindow( parent )
{
}


In addition, the "bilder" variable is defined incorrectly. It should be QList<QString>, not QList<QString *>. This will cause compiler errors in the "setzeBilder" function because QString::fromStdString() returns a QString, not a QString *.

And why does your "erzeugen()" method have a Levels * argument that is never used? Looks to me like you don't understand how to create and initialize a QWidget-based class. Everything that you are doing in these two extra methods should be called from your class constructor, not externally, like this:



Levels::Levels( QWidget * parent )
: QMainWindow( parent )
{
setzeBilder();
erzeugen(); // Note, "Levels *" argument is unnecessary and is removed.
}


And there are probably 6 or 7 more serious errors and memory leaks in your code as well which I will let you discover on your own.


Is there a way to set the list at the class where this class is called?

I have no idea what this question means.

ZikO
20th September 2012, 22:18
Is there a way to set the list at the class where this class is called?
If you mean to create an object and initialize a list when an object of your class that contains that list is being created then you can probably use a constructor of your class with one more parameter.

joshy198
22nd September 2012, 14:16
levels.h

#ifndef MYWINDOW_H
#define MYWINDOW_H

#include <QPushButton>
#include <QMessageBox>
#include <QMainWindow>
#include <QHBoxLayout>
#include "QScrollArea"
#include "QImage"
#include "QLabel"
#include "QWidget"
#include "QVBoxLayout"
#include "QGroupBox"
#include "QSpacerItem"
#include "QList"

class Levels: public QMainWindow
{
Q_OBJECT

public:
Levels(){};
~ Levels(){};
void erzeugen();
void setzeBilder();
private:
QVBoxLayout *ramen;
QVBoxLayout *layout;
QImage *image;
QList<QLabel *> labels;
QPushButton *back;
QWidget *central;
QGroupBox *box;
QScrollArea *area;
QSpacerItem *space;
QList<QString> bilder;


//public slots:
};
#endif






#include "levels.h"

void Levels::erzeugen()
{
setzeBilder();
central=new QWidget(this);
ramen= new QVBoxLayout(central);
setCentralWidget(central);
box = new QGroupBox;
back = new QPushButton(" ? Back");
for(int i=0;i<12;i++)
{
labels << new QLabel();
}
for(int i=0;i<12;i++)
{
labels.at(i)->setBackgroundRole(QPalette::Dark);
labels.at(i)->setAutoFillBackground(true);
image = new QImage(bilder.at(i));//Muss im debug-Ordner sein!!!
labels.at(i)->setPixmap(QPixmap::fromImage(*image));
labels.at(i)->setMaximumHeight(image->height());
labels.at(i)->setMaximumWidth(image->width());
}

space = new QSpacerItem(1,10);
for(int i=0;i<12;i++)
{
layout->addWidget(labels.at(i));
}


box->setLayout(layout);
area = new QScrollArea;
area->setWidgetResizable(false);
area->setWidget(box);

}
void Levels::setzeBilder()
{
bilder.at(0)=QString::fromStdString("l01.png");
bilder.at(1)=QString::fromStdString("l02.png");
bilder.at(2)=QString::fromStdString("l03.png");
bilder.at(3)=QString::fromStdString("l04.png");
bilder.at(4)=QString::fromStdString("l05.png");
bilder.at(5)=QString::fromStdString("l06.png");
bilder.at(6)=QString::fromStdString("l07.png");
bilder.at(7)=QString::fromStdString("l08.png");
bilder.at(8)=QString::fromStdString("l09.png");
bilder.at(9)=QString::fromStdString("l10.png");
bilder.at(10)=QString::fromStdString("l11.png");
bilder.at(11)=QString::fromStdString("l12.png");

}


Errors

8237

@ZikO
If i make the QList public and include the levels.h in another class (the class from where levels is called), can i add elements to the QList directly in the class where i initialize levels?
@d_stranz
I don't really know how to initialize it your way. I know I'm quite bad at c++ :(
Normaly I'm develloping in java or c# (much easyer, but my nokia device does't support java oder c#)

wysota
22nd September 2012, 14:52
Please read again what Chris posted regarding the use of QList::at()

ZikO
24th September 2012, 11:00
Is there a way to set the list at the class where this class is called?



If i make the QList public and include the levels.h in another class (the class from where levels is called), can i add elements to the QList directly in the class where i initialize levels?

I think I know what you meant by writing the first sentence. You probably mean to initialize a list in run-time. I don't think it can help if you do what you say in the second sentence. To be honest, Chris already explained this and another problem and wrote the complete answer.


I read your first post along with the code and the errors that you have provided and I can only add that in C++ const is used in order to prevent from changing or assigning anything to an object returned from any function as a reference; it prevents from using such objects as so-called L-value. QList::at() returns a const reference:


const T & QList::at ( int i ) const

This is why the compiler complains about discarding qualifier here:


bilder.at(0)=QString::fromStdString("l01.png");

Bilder.at(0) etc. is at the left of "=". But bear in mind you have also the other problem that Chris also refers to.