Home > Software engineering >  Can't add '0' as a shortcut to Qt app, others work
Can't add '0' as a shortcut to Qt app, others work

Time:08-25

I'm making a calculator and I wan't to make the 0-9 digits capable to be entered from the keyboard. So I've created QPushButton's connected them with my slot and to each button I added a shortcut (0-9). For all other buttons I allso added shortcuts and they all work. Only the '0' one doesn't. I've been adding them in the Qt Design so I just entered the desired shortcut into a window.

Button0 property editor

Buttons are named Button0,..., Button9 and are connected this way:

ui->Display->setText(QString::number(calcVal, 'g', span));
    QPushButton *numButtons[10];
    for(int i = 0; i < 10;   i)
    {
        QString butName = "Button"   QString::number(i);
        numButtons[i] = Calculator::findChild<QPushButton *>(butName);
        connect(numButtons[i], SIGNAL(released()), this,
                SLOT(NumPressed()));
    }//for

The function looks like that:

void Calculator::NumPressed()
{
    if (nowy)
    {
        ui->Display->setText("");
        nowy = false;
    }//if
    if (mathBut)
    {
        ui->Display->setText("");
        mathBut = false;
    }//if
    QPushButton *button = (QPushButton *)sender();
    butVal = button->text();
    displayVal = ui->Display->text();
    QString zero = "0";
    if(displayVal == zero)
        ui->Display->setText(butVal);
    else
        ui->Display->setText(displayVal.append(butVal));

    rBracket = false;
}//void

But I don't think that that's the problem because if I click the button by mouse it works properly only the shortcut doesn't work while it's '0'. When I changed it to e.g. 'K' or 'J' it worked.

The shortcut is assigned in the xml file:

<widget  name="Button0">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>280</y>
      <width>91</width>
      <height>32</height>
     </rect>
    </property>
    <property name="sizePolicy">
     <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
      <horstretch>0</horstretch>
      <verstretch>0</verstretch>
     </sizepolicy>
    </property>
    <property name="styleSheet">
     <string notr="true">QPushButton {
    background-color: Silver;
    border: 1px solid gray;
    padding: 5px;
    color: black;
}

QPushButton:pressed {
    background-color: #A9A9A9;
    border: 1px solid gray;
    padding: 5px;
    color: black;
}</string>
    </property>
    <property name="text">
     <string>0</string>
    </property>
    <property name="shortcut">
     <string>0</string>
    </property>
   </widget>

(line 34-35)

Can anyone help?

CodePudding user response:

The only think I can think of: the 0 shortcut is already assigned to something else in that same widget. Maybe you could open the ui file in edit mode and find <string>0</string>, using Select All, then scroll the file top to bottom to find the 'shadowing' one.

A (maybe) quicker way: notice if Button0 at least takes focus when zero is pressed, then lose it when zero is pressed again. If yes: try to visually track the focus (i.e. see to what other control is being assigned).

  • Related