Problems by inserting Images in a QLabel
in Levels.h
Code:
#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"
{
Q_OBJECT
public:
Levels(){};
~ Levels(){};
void erzeugen(Levels *level);
void setzeBilder();
private:
QList<QLabel *> labels;
QList<QString *> bilder;
//public slots:
};
#endif
in levels.cpp
Code:
#include "levels.h"
void Levels::erzeugen(Levels *level)
{
setzeBilder();
setCentralWidget(central);
for(int i=0;i<12;i++)
{
}
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());
}
for(int i=0;i<12;i++)
{
layout->addWidget(labels.at(i));
}
box->setLayout(layout);
area->setWidgetResizable(false);
area->setWidget(box);
}
void Levels::setzeBilder()
{
for(int i=0;i<12;i++)
{
}
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?
Re: Problems by inserting Images in a QLabel
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*
Quote:
Is there a way to set the list at the class where this class is called?
I do not know what this means.
Re: Problems by inserting Images in a QLabel
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:
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:
Code:
{
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.
Quote:
Is there a way to set the list at the class where this class is called?
I have no idea what this question means.
Re: Problems by inserting Images in a QLabel
Quote:
Originally Posted by
joshy198
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.
1 Attachment(s)
Re: Problems by inserting Images in a QLabel
levels.h
Code:
#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"
{
Q_OBJECT
public:
Levels(){};
~ Levels(){};
void erzeugen();
void setzeBilder();
private:
QList<QLabel *> labels;
QList<QString> bilder;
//public slots:
};
#endif
Code:
#include "levels.h"
void Levels::erzeugen()
{
setzeBilder();
setCentralWidget(central);
for(int i=0;i<12;i++)
{
}
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());
}
for(int i=0;i<12;i++)
{
layout->addWidget(labels.at(i));
}
box->setLayout(layout);
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
Attachment 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#)
Re: Problems by inserting Images in a QLabel
Please read again what Chris posted regarding the use of QList::at()
Re: Problems by inserting Images in a QLabel
Quote:
Originally Posted by
joshy198
Is there a way to set the list at the class where this class is called?
Quote:
Originally Posted by
joshy198
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:
Code:
const T
& QList::at ( int i
) const
This is why the compiler complains about discarding qualifier here:
Code:
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.