Results 1 to 9 of 9

Thread: Problem with QT code.

  1. #1
    Join Date
    Sep 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Problem with QT code.

    I recently started using QT for a project that I ahve to do and to get a feel for the software I followed a youtube tutorial on making a calculator. But the problem is that when I read a push buttons text it reads it in with the & sign and that is preventing the calculator from working correctly.

    (QPushButton* button = (QPushButton*) sender(); and then button->text() )

    Does anyone know why this is happening and how to fix it?

    Thank you in advance.

  2. #2
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with QT code.

    Maybe not the direct answer to your question, but why do you need to read the button text?

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with QT code.

    why do you need to read the button text?
    Sounds like the OP has connected the clicked() signal for all of the buttons to the same slot and is trying to use sender() to determine which button was clicked.

    @OP: Where does this "&" symbol come from? Are you adding it to the button text as a shortcut of some kind? Whatever you add with setText() (either in code or in the Qt Designer) will be returned to you with the call to text().

    If you want to continue with this design (which is OK for a beginning project but not something I would recommend for real code), then read the text into a QString. If a call to QString::contains() returns true for the character '&', then use QString::remove() to get a new QString with the offending character removed. Note that remove() will remove -all- '&' characters. If that isn't what you want, then you can manually find and remove the one you want using combinations of the QString methods indexOf(), length(), left(), mid(), and right().

    If I were going to do this, I would use QSignalMapper. The example in the docs does almost exactly what you are trying to do, but I would replace a mapping based on text to a mapping based on a unique integer for each button. In the slot connected to the QSignalMapper::mapped() signal, I would look up the integer in an array (position 0 = '0', position 1 = '1', ..., position 10 = '+', etc.) and use those to build up the expression to be evaluated. The advantage here is that you are independent of the text on the button. For a simple calculator this won't change with the native language of the user, but if you were doing something where the text was translated into some other language, the dependency your code now has on the actual text of the button would no longer exist.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Sep 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Problem with QT code.

    Thank you for the suggestion, I will look into it.

  5. #5
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with QT code.

    Do not read button texts. You will be forced to interpret the texts, moreover, you can read them wrongly.
    Solution #1: Each button has its own instance. Compare button with the instance addresses:
    Qt Code:
    1. QPushButton *button = dynamic_cast<QPushButton *>(sender());
    2.  
    3. if( button == &button0 ) // the '0' button
    4. {
    5. do '0' button;
    6. }
    7. else if( button == &button1 )
    8. {
    9. etc;
    10. }
    11. ...
    12. else
    13. {
    14. report error;
    15. }
    To copy to clipboard, switch view to plain text mode 
    Solution #1 needs that your slot is a member of a class where all buttons are reachable (the calculator frame most likely).

    Solution #2 (simplified d_stranz solution). Derive a class, say myButton, from QPushButton and add suitable data to myButton. Populate your calculator with myButtons. When you get a signal, check your data and see which button was it.
    Qt Code:
    1. class myButton : public QPushButton
    2. {
    3. public :
    4.  
    5. int myID;
    6.  
    7. myButton( QWidget *parent = nullptr );
    8. ... add suitable ctors;
    9. virtual ~myButton();
    10. }
    11.  
    12. myButton::myButton( QWidget *parent ) : QPushButton(parent)
    13. {
    14. }
    15.  
    16. ...
    17.  
    18. myButton::~myButton()
    19. {
    20. }
    To copy to clipboard, switch view to plain text mode 

    Set IDs for all buttons (naturally, you can do it in your ctors). Populate your calculator with myButtons. Now:
    Qt Code:
    1. myButton button = dynamic_cast<myButton *>(sender());
    2.  
    3. if( button == nullptr ) report error, it wasn't myButton;
    4. else
    5. {
    6. switch( button->myID )
    7. {
    8. case button0 :
    9. {
    10. }
    11. ...
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Sep 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Problem with QT code.

    Thank you for the replays.

    A weird thing that I have found is that the code works perfect in Windows QTCreator but not in Manjaro Linux. In Manjaro it adds the & sign to the symbols read from the pushButton.

    Also when I tried to cast something to a QPushButton type in Manjaro it did not recognise it in the ide (it did not change the text color to match other Q type stuff). Could the problem then be my Linux distro?

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,315
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with QT code.

    Could the problem then be my Linux distro?
    You mean your linux OS itself? Not likely. That's like junking your car because you ran out of gas. You don't need a new car, you need more gas.

    It is possible that the implementation of Qt on your platform is a bit strange. Each platform has a Qt driver that interfaces the Qt code to low-level graphics in the OS. These DLLs / shared libraries are found in the plugins/platforms subdirectory of your Qt distribution. The one for Windows is called "qwindows.dll". It could be that the problem with the "&" is somewhere in this layer for your linux distro. Otherwise, no idea.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #8
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    518
    Thanks
    13
    Thanked 77 Times in 75 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QT code.

    Hi, the & in the text of a button is used to define the keyboard shortcut. If you press ALT the corresponding character should be underlined or otherwise highlighted. Maybe the linux version returns this marker.

    Ginsengelf

  9. #9
    Join Date
    Sep 2017
    Posts
    4
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Problem with QT code.

    I got it working. The problem was the version that is installed using Octopi in Manjaro did not work. Reinstalled it using the website version and it worked.

Similar Threads

  1. I am having a minor problem with my code.
    By ayanda83 in forum Qt Programming
    Replies: 5
    Last Post: 5th April 2015, 17:06
  2. There is a problem with this code
    By rezas1000 in forum Newbie
    Replies: 7
    Last Post: 31st August 2014, 17:47
  3. There is a problem in the code
    By rezas1000 in forum Newbie
    Replies: 8
    Last Post: 25th August 2014, 14:01
  4. uic generate code problem.
    By jerry7 in forum Newbie
    Replies: 1
    Last Post: 21st March 2011, 07:59
  5. Problem with code (probably repaintinng label)
    By hakermania in forum Newbie
    Replies: 2
    Last Post: 8th January 2011, 13:58

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.