Label skalieren??

Alles rund um die Programmierung mit Qt
nici
Beiträge: 246
Registriert: 29. Oktober 2008 12:50

Label skalieren??

Beitrag von nici »

hallo,

ich lade ein Bild und möchte es gern skalieren können.
Mit setScaledContents(); funktioniert es nicht. Bei QLabel hab ich auch nichts gefunden. (ausser setPicture)

lg

nici
Hmm mir fällt nichts ein ^^
jerry42
Beiträge: 126
Registriert: 9. Oktober 2008 10:48

Beitrag von jerry42 »

Hallo,

vielleicht könntest du noch etwas präziser schreiben, was du letztlich vorhast?
Soll das Bild einfach an den Platz angepasst werden, oder soll nachher per Benutzerinteraktion das Bild größer und kleiner werden?

Du lädst ja ein QPixmap auf dein QLabel.
Vielleciht helfen dir folgende Methoden von QPixmap weiter (siehe http://doc.trolltech.com/4.4/qpixmap.html):


QPixmap scaled ( const QSize & size, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const

QPixmap scaled ( int width, int height, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio, Qt::TransformationMode transformMode = Qt::FastTransformation ) const

QPixmap scaledToHeight ( int height, Qt::TransformationMode mode = Qt::FastTransformation ) const

QPixmap scaledToWidth ( int width, Qt::TransformationMode mode = Qt::FastTransformation ) const

Gruß jerry42
upsala
Beiträge: 3946
Registriert: 5. Februar 2006 20:52
Wohnort: Landshut
Kontaktdaten:

Beitrag von upsala »

Was funktioniert nicht? Wie sieht der Code dazu aus?
nici
Beiträge: 246
Registriert: 29. Oktober 2008 12:50

Beitrag von nici »

hallo,

also ich möchte, dass der Benutzer ein Bild laden kann und nach dem er das Bild geladen hat, soll er es mit einem Slider oder Spinbox vergrößern und verkleinern können.
Meine Methode sieht so aus:

Code: Alles auswählen

void TabEins::loadPic() 
 { 												 
	QString fileName = QFileDialog::getOpenFileName(this, "Open File", "/home",("Images(*.png *.gif *.jgp *.jpeg *.bmp *.tiff *.xbm *.xpm)"));
	if(!fileName.isEmpty()){											  
 	QImage file(fileName);
    if(file.isNull()) { qWarning()<<"Fehler beim öffnen der Datei!";
	}
	qWarning() << "ok";
	imageLabel->setPixmap(QPixmap::fromImage(file));
	}	
 }
das mit

Code: Alles auswählen

//imageLabel->setScaledContents(true);
habe ich raus genommen.
(ich guck mir mal QPixmap an ), danke


lg
nici
Hmm mir fällt nichts ein ^^
nici
Beiträge: 246
Registriert: 29. Oktober 2008 12:50

Beitrag von nici »

ich glaube mein connect zwischen dem lable und der spinBox ist nicht richtig.

Code: Alles auswählen

connect(spinBox, SIGNAL(valueChanged(int)),
			imageLabel, SLOT(setPicture()));
weil das hier dann kommt.

Object::connect: No such slot QLabel::setPicture()
Object::connect: (sender name: 'spinBox')
Object::connect: (receiver name: 'imageLabel')

lg

nici
Hmm mir fällt nichts ein ^^
upsala
Beiträge: 3946
Registriert: 5. Februar 2006 20:52
Wohnort: Landshut
Kontaktdaten:

Beitrag von upsala »

Hübsch, und so eine aussagekräftige Fehlermeldung. Dann lies dir endlich mal die Doku dazu durch. Oder sollen wir das wieder für dich tun?
nici
Beiträge: 246
Registriert: 29. Oktober 2008 12:50

Beitrag von nici »

ja das wäre nett, ey als ob ich das nicht machen würde. Aber die doku hilft mir überhaupt nicht, wie oft soll ich das denn noch sagen, ich brauch immer ein Beispiel oder einen kleinen Anfang, dann gehts bei mir. aber ich werde jetzt mal googlen, bevor wieder so freche und unhöfliche Antworten kommen. Antwortet doch einfach nicht, wenn ihr mir jedesmal sagt, "lern c++", oder "steht in der Doku", als ob ich da nicht als erstes geguckt hätte. Wenn ich mal so erfahren bin wie euch, dann werde ich den schwächeren richtig helfen.

lg

nici
Hmm mir fällt nichts ein ^^
AuE
Beiträge: 918
Registriert: 5. August 2008 10:58

Beitrag von AuE »

und was steht in der doku welche slots dein QLAbel hat??? Wie sind die Slots definiert????
??? Etwa so???

Code: Alles auswählen

QLabel::setPicture()
???

das steht garantiert in der doku.... wie auch ein beispiel zu signal slots.
ausserdem is hier wöchentlich ein thread zum thema s/s prinzip/problem


Und in der Doku sthet das hier MIT Beispiel zum dran lang hangeln:
A Small Example
A minimal C++ class declaration might read:

Code: Alles auswählen

class Counter
 {
 public:
     Counter() { m_value = 0; }

     int value() const { return m_value; }
     void setValue(int value);

 private:
     int m_value;
 };
A small QObject-based class might read:

Code: Alles auswählen

#include <QObject>

 class Counter : public QObject
 {
     Q_OBJECT

 public:
     Counter() { m_value = 0; }

     int value() const { return m_value; }

 public slots:
     void setValue(int value);

 signals:
     void valueChanged(int newValue);

 private:
     int m_value;
 };
The QObject-based version has the same internal state, and provides public methods to access the state, but in addition it has support for component programming using signals and slots. This class can tell the outside world that its state has changed by emitting a signal, valueChanged(), and it has a slot which other objects can send signals to.
All classes that contain signals or slots must mention Q_OBJECT at the top of their declaration. They must also derive (directly or indirectly) from QObject.
Slots are implemented by the application programmer. Here is a possible implementation of the Counter::setValue() slot:

Code: Alles auswählen

 void Counter::setValue(int value)
 {
     if (value != m_value) {
         m_value = value;
         emit valueChanged(value);
     }
 }
The emit line emits the signal valueChanged() from the object, with the new value as argument.
In the following code snippet, we create two Counter objects and connect the first object's valueChanged() signal to the second object's setValue() slot using QObject::connect():

Code: Alles auswählen

 Counter a, b;
     QObject::connect(&a, SIGNAL(valueChanged(int)),
                      &b, SLOT(setValue(int)));

     a.setValue(12);     // a.value() == 12, b.value() == 12
     b.setValue(48);     // a.value() == 12, b.value() == 48
Calling a.setValue(12) makes a emit a valueChanged(12) signal, which b will receive in its setValue() slot, i.e. b.setValue(12) is called. Then b emits the same valueChanged() signal, but since no slot has been connected to b's valueChanged() signal, the signal is ignored.
Note that the setValue() function sets the value and emits the signal only if value != m_value. This prevents infinite looping in the case of cyclic connections (e.g., if b.valueChanged() were connected to a.setValue()).
A signal is emitted for every connection you make; if you duplicate a connection, two signals will be emitted. You can always break a connection using QObject::disconnect().
This example illustrates that objects can work together without needing to know any information about each other. To enable this, the objects only need to be connected together, and this can be achieved with some simple QObject::connect() function calls, or with uic's automatic connections feature.

und in der Doku zum QLabel stehen Folgende Slots:
Public Slots
void clear ()
void setMovie ( QMovie * movie )
void setNum ( int num )
void setNum ( double num )
void setPicture ( const QPicture & picture )
void setPixmap ( const QPixmap & )
void setText ( const QString & )
19 public slots inherited from QWidget
1 public slot inherited from QObject
nici
Beiträge: 246
Registriert: 29. Oktober 2008 12:50

Beitrag von nici »

ok AuE, danke. Ich weiss ja wie das mit dem connect funkt, aber ich glaube dass das Problem bei meinem Designer liegt. Da hat das Label ja eine größe, und kann man die dann einfach mit einem slider oder spinBox Ändern??
(aber ehrlich gesagt, weiss ich ja nicht ob ich das lable richtig connected habe, ich guck mir das beispiel an, obwoh ich das glaub vorher schon gesehen hatte ^^).
Ich möchte ja drei Sachen miteinander verbinden.

lg

nici
Hmm mir fällt nichts ein ^^
AuE
Beiträge: 918
Registriert: 5. August 2008 10:58

Beitrag von AuE »

Ich weiss ja wie das mit dem connect funkt
NEIN!!!!
ich glaube dass das Problem bei meinem Designer liegt
Glaube ich nicht!
größe, und kann man die dann einfach mit einem slider oder spinBox Ändern??
Ja klar, wenn man das mit den S/S verstanden hat!
Ich möchte ja drei Sachen miteinander verbinden.
Die da wären?
AuE
Beiträge: 918
Registriert: 5. August 2008 10:58

Beitrag von AuE »

No such slot QLabel::setPicture()
und wie isses definiert???
void setPicture ( const QPicture & picture )
und wieso gehts dann nicht....
nici
Beiträge: 246
Registriert: 29. Oktober 2008 12:50

Beitrag von nici »

hmm ok,

Code: Alles auswählen

connect(spinBox, SIGNAL(valueChanged(int)),
			imageLabel, SLOT(setPicture( const QPicture & picture) ));
geht, also es kommt keine Fehlermeldung (vorher kam schon eine, weil ich den Parameter vergessen hatte )

ich möchte die SpinBox, den Slider und das Label miteinander verbinden. Wenn ich ein Bild in das Lable lade, dann möchte ich es mit dem Slider oder der SpinBox skalieren können.
Ich weiss jetzt kommt wieder eine bescheuerte Frage, arbeite in meiner loadPic() Methode mit QPixmap, sollte ich das nicht auch anstatt QPicture nehmen?

lg

nici
Hmm mir fällt nichts ein ^^
AuE
Beiträge: 918
Registriert: 5. August 2008 10:58

Beitrag von AuE »

We assume that you already know C++ and will be using it for Qt development. See the Qt website for more information about using other programming languages with Qt.
The best way to learn Qt is to read the official Qt book, C++ GUI Programming with Qt 4, Second Edition (ISBN 0-13-235416-0). This book provides comprehensive coverage of Qt programming all the way from "Hello Qt" to advanced features such as multithreading, 2D and 3D graphics, networking, item view classes, and XML. (The first edition, which is based on Qt 4.1, is available online.)
If you want to program purely in C++, designing your interfaces in code without the aid of any design tools, take a look at the Tutorials. These are designed to get you into Qt programming, with an emphasis on working code rather than being a tour of features.
If you want to design your user interfaces using a design tool, then read at least the first few chapters of the Qt Designer manual.
By now you'll have produced some small working applications and have a broad feel for Qt programming. You could start work on your own projects straight away, but we recommend reading a couple of key overviews to deepen your understanding of Qt: Qt Object Model and Signals and Slots.
At this point, we recommend looking at the overviews and reading those that are relevant to your projects. You may also find it useful to browse the source code of the examples that have things in common with your projects. You can also read Qt's source code since this is supplied.


Getting an Overview
If you run the Examples and Demos Launcher, you'll see many of Qt's widgets in action.
The Qt Widget Gallery also provides overviews of selected Qt widgets in each of the styles used on various supported platforms.

Qt comes with extensive documentation, with hypertext cross-references throughout, so you can easily click your way to whatever interests you. The part of the documentation that you'll probably use the most is the API Reference. Each link provides a different way of navigating the API Reference; try them all to see which work best for you. You might also like to try Qt Assistant: this tool is supplied with Qt and provides access to the entire Qt API, and it provides a full text search facility.
There are also a growing number of books about Qt programming; see Books about Qt Programming for a complete list of Qt books, including translations to various languages.
Another valuable source of example code and explanations of Qt features is the archive of articles from Qt Quarterly, a quarterly newsletter for users of Qt.
For documentation on specific Qt modules and other guides, refer to All Overviews and HOWTOs.
Good luck, and have fun!
AuE
Beiträge: 918
Registriert: 5. August 2008 10:58

Beitrag von AuE »

Code: Alles auswählen

connect(spinBox, SIGNAL(valueChanged(int)), 
         imageLabel, SLOT(setPicture( const QPicture & picture) ));
Wie soll das bitte gehen???? Du verbindest ein Signal das einen int weitergibt mit einer Fkt die ein QPicture erwartet....

Ich dachte du hast S/S verstanden???
nici
Beiträge: 246
Registriert: 29. Oktober 2008 12:50

Beitrag von nici »

das dachte ich auch, verdammt.
ich kann doch nicht setPicture(int) hinschreiben, oder?
lg
nici
Hmm mir fällt nichts ein ^^
Antworten