Results 1 to 12 of 12

Thread: Can someone explain this code from C++ GUI Programming with Qt 4?

  1. #1
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question Can someone explain this code from C++ GUI Programming with Qt 4?

    I am reading the C++ GUI Programming with Qt4 2nd Edition book trying to learn Qt. I am completely new to Qt and C++. I have experience with C#, Java, VB, Python, and PHP, but C++ is just so new right now that I'm having trouble figuring out how stuff works. The worst part is I don't know how to Google something like this. Anyways, to my question...

    I'm on Chapter 5, Creating Custom Widgets where you subclass QSpinBox in order to create HexSpinBox. This is the code I am referring to:
    Qt Code:
    1. #ifndef HEXSPINBOX_H
    2. #define HEXSPINBOX_H
    3.  
    4. #include <QSpinBox>
    5.  
    6.  
    7. class HexSpinBox : public QSpinBox
    8. {
    9. Q_OBJECT
    10. public:
    11. HexSpinBox(QWidget *parent = 0);
    12.  
    13. protected:
    14. QValidator::State validate(QString &text, int &pos) const;
    15. int valueFromText(const QString &text) const;
    16. QString textFromValue(int value) const;
    17.  
    18. private:
    19. QRegExpValidator *validator;
    20. };
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 

    My question is about the line
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    What does this line do? Why am I not doing something like a
    Qt Code:
    1. #include <QRegExpValidator>
    To copy to clipboard, switch view to plain text mode 
    instead? This is probably a trivial thing for someone with more experience, so I'm sorry if this is a stupid question. I'm sure I'll have several more simple questions as I keep reading.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    Quote Originally Posted by deejross View Post
    My question is about the line
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    What does this line do?
    That line is declaring a class (it is called a forward declaration), basically you just tell the compiler that you have a class called QRegExpValidator and it (the compiler) will get the definition a little bit later, more exactly in the .cpp file.

    You can use that technique, but be careful you can't create objects, you can only declare pointers and initialize the pointers only after the class has been defined by you or you included the framework file which define that class.
    Quote Originally Posted by deejross View Post
    Why am I not doing something like a
    Qt Code:
    1. #include <QRegExpValidator>
    To copy to clipboard, switch view to plain text mode 
    instead? This is probably a trivial thing for someone with more experience, so I'm sorry if this is a stupid question. I'm sure I'll have several more simple questions as I keep reading.
    You will do that in the .cpp file. (If not maybe the author simplified some things and included all QtGui module, not the all the .h files one by one).

  3. #3
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    This is the entire .cpp file:
    Qt Code:
    1. #include "hexspinbox.h"
    2.  
    3. HexSpinBox::HexSpinBox(QWidget *parent) :
    4. QSpinBox(parent)
    5. {
    6. this->setRange(0, 255);
    7. validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1-8}"), this);
    8. }
    9.  
    10. QValidator::State HexSpinBox::validate(QString &text, int &pos) const
    11. {
    12. return validator->validate(text, pos);
    13. }
    14.  
    15. QString HexSpinBox::textFromValue(int val) const
    16. {
    17. return QString::number(val, 16).toUpper();
    18. }
    19.  
    20. int HexSpinBox::valueFromText(const QString &text) const
    21. {
    22. bool ok;
    23. return text.toInt(&ok, 16);
    24. }
    To copy to clipboard, switch view to plain text mode 

    QRegExpValidator is not defined there either, which is why I'm wondering if there's some kind of "magic" going on somewhere. I've been looking at some of Qt's .h files and a lot of things in there are way out of my league right now, but I did notice that QAbstractSpinBox does an #import <QValidator>, so I'm wondering if QRegExpValidator is already declared somewhere by QSpinBox and it's using a forward declaration as a way to keep QRegExpValidator from being imported more than once. Does that make sense, or am I totally misunderstanding this stuff?

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    And that code compiles?

    Are you sure you don't have something like:
    Qt Code:
    1. #include <QtGui> //which include all QtGui module (in that is QRegExpValidator defined)
    2. #include "hexspinbox.h"
    To copy to clipboard, switch view to plain text mode 
    ?

  5. #5
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    I am positive. I have posted the full .h and full .cpp that I have and the code does compile without any errors. It took me a few minutes to figure out how to add the widget to a QMainWindow in order to test that it actually works. And it does work. From what I've read so far, this should NOT be working, but it is, so I've somehow missed the reason it works.

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    Then it must be the QSpinBox that include the QRegExpValidator (it has by default some validation tools, maybe it uses internally QRegExpValidator).
    Quote Originally Posted by deejross View Post
    ...and it's using a forward declaration as a way to keep QRegExpValidator from being imported more than once.
    No, the include guards are for that:
    Qt Code:
    1. #ifndef HEXSPINBOX_H
    2. #define HEXSPINBOX_H
    3. //... your class declaration
    4. #endif
    To copy to clipboard, switch view to plain text mode 
    The forward declaration are used to decrease the compile time, since the framework headers are not included in more of your files, in case you include your header hexspinbox.h in many other of your files.
    And other usage is when you have two classes that each use pointer to the other.
    See this FAQ

  7. The following user says thank you to Zlatomir for this useful post:

    deejross (20th January 2011)

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    Suprisingly QSpinBox does include QRegExpValidator through inclusion of qabstractspinbox.h which in turn includes qvalidator.h which defines QRegExpValidator.

    Qt Code:
    1. #include <QSpinBox>
    2.  
    3. int main(int argc, char **argv){
    4. return 0;
    5. }
    To copy to clipboard, switch view to plain text mode 

    This compiles which means the forward declaration from the original snippet is not needed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    deejross (20th January 2011)

  10. #8
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    Quote Originally Posted by wysota View Post
    This compiles which means the forward declaration from the original snippet is not needed.
    You are correct. I commented out the class QRegExprValidator; line in my .h file and the project still builds and works. Thanks.

  11. #9
    Join Date
    Mar 2011
    Location
    Madrid
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    Hi all,

    I am also following this book and I have a question regarding this exact same example.

    I was trying to play with it making it display the prefix "0x" by adding the line
    Qt Code:
    1. setPrefix("0x")
    To copy to clipboard, switch view to plain text mode 
    to the HexSpinbox default constructor as so:

    Qt Code:
    1. HexSpinBox::HexSpinBox (QWidget *parent)
    2. : QSpinBox(parent)
    3. {
    4. setPrefix("0x");
    5. setRange(0,255);
    6. validator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,8}"), this);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Unfortunately this compiles but immediately gives me a seg fault:

    The inferior stopped because it received a signal from the Operating System.

    Signal name : SIGSEGV
    Signal meaning : Segmentation fault
    Does this have anything to do with the validator getting the prefix value? Is it fixable?

    Thanks in advance,
    Manu

  12. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    Try setting the prefix after you initialize *validator.

  13. The following user says thank you to norobro for this useful post:

    portilhe (17th March 2011)

  14. #11
    Join Date
    Mar 2011
    Location
    Madrid
    Posts
    10
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    Thanks, it works. Well, almost! I had to change the regular expression to
    Qt Code:
    1. validator = new QRegExpValidator(QRegExp("0x[0-9A-Fa-f]{1,8}"), this);
    To copy to clipboard, switch view to plain text mode 
    otherwise, although it compiled and worked fined with the arrows, it didn't accept correctly any keyed-in input.

    So now I have 2 questions:
    1) Why does it work setting the prefix after the validator and not before?
    2) From the fact that I had to include the "0x" inthe regular expression I am guessing that the reimplemented function (from QSpinBox) HexSpinBox::validate gets as a first argument the full text of the spin box, whereas the function valueFromText gets a stripped version. Am I guessing wrong?

  15. #12
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Can someone explain this code from C++ GUI Programming with Qt 4?

    Why does it work setting the prefix after the validator and not before?
    setPrefix() calls HexSpinBox::validate(). In the return statement validate is an uninitialized pointer which causes a segfault.

    2) ... Am I guessing wrong?
    Run your code in a debugger or put in some qDebug() statements to see what's happening.

Similar Threads

  1. Programming patterns, code structure? C++, Qt & wxwidgets
    By nardev in forum General Programming
    Replies: 30
    Last Post: 10th December 2010, 15:48
  2. Replies: 3
    Last Post: 4th June 2010, 08:10
  3. "C++ GUI Programming with Qt 4" book example code
    By ia32 in forum General Discussion
    Replies: 4
    Last Post: 1st June 2010, 22:51
  4. Can't explain these errors
    By Petr_Kropotkin in forum Newbie
    Replies: 4
    Last Post: 23rd January 2010, 19:06
  5. could somebody please explain const to me?
    By mikro in forum General Programming
    Replies: 4
    Last Post: 29th September 2006, 14:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.