PDA

View Full Version : Problem with QT code.



Rukkhar
2nd September 2017, 06:57
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.

ado130
2nd September 2017, 13:40
Maybe not the direct answer to your question, but why do you need to read the button text?

d_stranz
2nd September 2017, 16:30
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.

Rukkhar
2nd September 2017, 16:37
Thank you for the suggestion, I will look into it.

Radek
2nd September 2017, 19:05
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:


QPushButton *button = dynamic_cast<QPushButton *>(sender());

if( button == &button0 ) // the '0' button
{
do '0' button;
}
else if( button == &button1 )
{
etc;
}
...
else
{
report error;
}

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.


class myButton : public QPushButton
{
public :

int myID;

myButton( QWidget *parent = nullptr );
... add suitable ctors;
virtual ~myButton();
}

myButton::myButton( QWidget *parent ) : QPushButton(parent)
{
}

...

myButton::~myButton()
{
}


Set IDs for all buttons (naturally, you can do it in your ctors). Populate your calculator with myButtons. Now:


myButton button = dynamic_cast<myButton *>(sender());

if( button == nullptr ) report error, it wasn't myButton;
else
{
switch( button->myID )
{
case button0 :
{
}
...
}
}

Rukkhar
2nd September 2017, 19:59
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?

d_stranz
3rd September 2017, 15:35
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.

Ginsengelf
4th September 2017, 08:18
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

Rukkhar
4th September 2017, 08:26
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.