简体   繁体   中英

How to have QLabel update as various numbered pushbuttons are clicked

I have a dialpad with numbers 1-9 and 0, and a QLabel above it to show the numbers when clicked(same as a keypad on any phone). All are push buttons. What is the easiest way to get the QLabel to show the numbers as the push buttons are clicked?

For example, if 2 then 0 then 7 is clicked, the label would update in real time with 207. The format of the Qlabel should follow standard phone numbers, 000-000-0000. I understand how to setText for one number at a time, but they keep overriding each other. Any help is appreciated. Thank you in advance

What you are looking for is a QSignalMapper . It maps multiple inputs through a single interface and does the sender dispatching for you.

   QSignalMapper *mapper(new QSignalMapper(parent));
   for (int i=0; i<10; ++i){
       QPushButton *button = some_new_button_function();
       connect(button, &QPushButton::clicked, mapper, &QSignalMapper::map);
       mapper->setMapping(button, i);
   }
   connect(mapper, QOverload<int>::of(&QSignalMapper::mapped), 
           [this](int i){/*here your append code*/});

The easiest is to connect the clicked signal of the buttons to a slot (possibly a lambda) that changes the text of the QLabel (using setText() ). If you want to append to the current text, then just do setText(label.text() + "new text"); .

You have to connect the signals clicked() emitted by each QPushButton to a slot that update the QLabel text.

A brief example

In the parent constructor:

connect(qpb1, &QPushButton::clicked, this, &MyClass::handleQLabel);

And the possible slot implementation:

void MyClass::handleQLabel()
{
    QPushButton * qpb = qobject_cast<QPushButton*>(sender()); // Find the sender of the signal
    if(qpb != nullptr)
        this->myLabel->setText(qpb->text()); // Write anything you want in the QLabel
    else
    {
        // Do what you want.
    }
}

This will do the job.

Of course if you don't want use sender() (for multi-threading concerns for example) you can either create one slot by QPushButton and do the same number of connect (heavy and quite a dirty workaround), or create a subclass of QPushButton to add a custom signal to emit with an identifier of the QPushButton and get it with a slot for example.

I hope it can help :)

QLineEdit might better suit your needs in this case if you also want your data representation to follow phone number standard such as "000-000-0000". You can make it read-only, disable interaction flags if you like (but from UI/UX perspective it is better not to, since mostly there is no reason to disallow copying), and also you can set input mask you like. Given your current situation, you can base your needs on the following example:

// Set your format.
ui->lineEdit->setInputMask("000-000-0000");

// Make sure that your text would be in the format you like initially.
ui->lineEdit->setText("999-999-9999");

// Text will be not editable.
ui->lineEdit->setReadOnly(true);

// And here, you can use QSignalMapper as other members have suggested. Or you can just connect multiple buttons somehow. The choice is yours to make.
connect(ui->pushButton, &QPushButton::clicked, ui->lineEdit, [this]
{
    // Just keep in mind taht when returning text, some of the mask elements might be here, too.
    ui->lineEdit->setText(ui->lineEdit->text().replace("-", "") + "1");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM