Labs for QT in Education
-
Anyone has a proper solution to the Lab2 for L5 and L6. I have a little problem, im new to -QT- Qt and i'm trying to incorporate some of these Labs into an intro. gui course. I'm not familiar with Q_PROPERTY and a section of the Lab deals with adding set and get in Q_PROPERTY for Value, minimum and maximum. How do I check if they are within the specified range?
-
I stuck on the part of the Lab 2 -custom widgets, graphics view and canvas. The section with Designing the API, it says
[quote]Repeat the process described for the value property to add the minimum and maximum properties of type QSize. Modify the setValue() slot to make sure the value always is within the valid range and implement setters of the new properties in such a way that they too make sure the value is constrained to the new range.[/quote]
What i don't understand is if we add Q_PROPERTY to minimum and maximum of type QSize and try to use qBound() it gives me error. How do I check that the value is within range. Can you use QSize with qBound()? any help on this section of the Lab would be welcomed... thanks.Edit: layout fixes; Andre
-
Hi,
qBound() for QSize is not defined because it is not easily determined whether (20,10) is larger than (10, 20).
You need to do the comparison dimension by dimension like that:
@void PuzzleSizeWidget::setValue(const QSize &s) {
// ...
QSize newSize;
newSize.setWidth(qBound(minimum().width(), s.width(), maximum().width());
newSize.setHeight(qBound(minimum().height(), s.width(), maximum().height());
// ...
}@or make use of QSize::boundedTo() and QSize::expandedTo():
@void PuzzleSizeWidget::setValue(const QSize &s) {
// ...
QSize newSize = s.expandedTo(minimum()).boundedTo(maximum());
// ...
}@Of course you could also just define qBound() for QSize:
@QSize qBound(const QSize& min, const QSize& curr, const QSize& max) {
return curr.boundedTo(max).expandedTo(min);
}@Hope that helps, good luck with your lab :)