Results 1 to 12 of 12

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


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

    deejross (20th January 2011)

  3. #2
    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.

  4. #3
    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

  5. #4
    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.

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

    portilhe (17th March 2011)

  7. #5
    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?

  8. #6
    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
  •  
Qt is a trademark of The Qt Company.