January 12, 2012

Manuel Medina Manuel Medina
Lab Rat
1 posts

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?

2 replies

January 13, 2012

Manuel Medina Manuel Medina
Lab Rat
1 posts

I stuck on the part of the Lab 2 -custom widgets, graphics view and canvas. The section with Designing the API, it says

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.

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

January 13, 2012

wysota wysota
Lab Rat
2 posts

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:

  1. void PuzzleSizeWidget::setValue(const QSize &s) {
  2.   // ...
  3.   QSize newSize;
  4.   newSize.setWidth(qBound(minimum().width(), s.width(), maximum().width());
  5.   newSize.setHeight(qBound(minimum().height(), s.width(), maximum().height());
  6.   // ...
  7. }

or make use of QSize::boundedTo() and QSize::expandedTo():

  1. void PuzzleSizeWidget::setValue(const QSize &s) {
  2.   // ...
  3.   QSize newSize = s.expandedTo(minimum()).boundedTo(maximum());
  4.   // ...
  5. }

Of course you could also just define qBound() for QSize:

  1. QSize qBound(const QSize& min, const QSize& curr, const QSize& max) {
  2.   return curr.boundedTo(max).expandedTo(min);
  3. }

Hope that helps, good luck with your lab :)

 
  ‹‹ Programming with Qt Quick for Symbian and MeeGo Harmattan Devices      Need help... confused a bit with Qt Creator ››

You must log in to post a reply. Not a member yet? Register here!