Results 1 to 8 of 8

Thread: QPushButton - setText with "\n" not working

  1. #1
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QPushButton - setText with "\n" not working

    Sorry, I'm quiet new in Qt and it's possible that somebody will laugh about this question, but I can't found a solution so I would like to ask you guys for al little bit help.

    Actually I'm using Qt 4.3 together with Linux and MySQL 5.

    From a MySql database I got "Hello\n World" using

    Qt Code:
    1. QString test1(query.value(1).toString())
    To copy to clipboard, switch view to plain text mode 

    In my database "Hello\n World" is stored as varschar(40).

    Then for testing I have tried this:

    Qt Code:
    1. QString test1(query.value(1).toString());
    2. QString test2 = "Hello\n World";
    3.  
    4. qDebug() << query.value(1); // QVariant(QString, "Hello\World")
    5. qDebug() << test1; // gives me "Hello\n World"
    6. qDebug() << test2; // gives me "Hello
    7. World"
    To copy to clipboard, switch view to plain text mode 

    How it comes, that in test1 the newline character is ignored?

    Thanks in advance

    Guenther
    Davao City, Philippines, Planet Earth
    Last edited by gboelter; 9th February 2008 at 08:02. Reason: reformatted to look better

  2. #2
    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: QPushButton - setText with "\n" not working

    Push buttons don't allow a multiline text. You can use my QwwRichTextButton class if you want or make your own subclass of the button.

  3. #3
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPushButton - setText with "\n" not working

    Quote Originally Posted by wysota View Post
    Push buttons don't allow a multiline text. You can use my QwwRichTextButton class if you want or make your own subclass of the button.
    Thanks wysota, I will try it.

    What I don't understand is, why it's working with
    Qt Code:
    1. QString test2 = "Hello\n World";
    To copy to clipboard, switch view to plain text mode 

    Here my Button has two lines ....?

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton - setText with "\n" not working

    Might be an encoding problem.
    Try with:
    Qt Code:
    1. QString test1(query.value(1).toString().toAscii().constData());
    To copy to clipboard, switch view to plain text mode 

    Also, in 4.3.3 QPushButtons support multiline strings, it's just they don't resize to the contents by default. See the attachment.
    Attached Images Attached Images

  5. #5
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPushButton - setText with "\n" not working

    [QUOTE=marcel;62576]Might be an encoding problem.
    Try with:
    Qt Code:
    1. QString test1(query.value(1).toString().toAscii().constData());
    To copy to clipboard, switch view to plain text mode 

    Sorry, still the same ....


  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPushButton - setText with "\n" not working

    I think the "\" in test1 might be escaped. It is the only explanation.
    Try :
    Qt Code:
    1. int c = test1.count('\');
    To copy to clipboard, switch view to plain text mode 
    See what is the value of c.

    If it is then you only need to remove one \ from the string.

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

    gboelter (9th February 2008)

  8. #7
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPushButton - setText with "\n" not working

    [QUOTE=marcel;62579]I think the "\" in test1 might be escaped. It is the only explanation.

    Thanks Marcel, you are right, that was the problem.

    Guenther

  9. #8
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPushButton - setText with "\n" not working

    Quote Originally Posted by wysota View Post
    You can use my QwwRichTextButton class if you want or make your own subclass of the button.
    Thanks again wysota, I have installed your class and I like it .....

Similar Threads

  1. setText not working
    By MarkoSan in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 13:05

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.