Установить маску ввода для QLabel? - PullRequest
1 голос
/ 07 мая 2019

Я понимаю, что QLineEdit использует setInputMask для форматирования.Мне было интересно, если QLabel было что-то подобное?

Я пытаюсь установить маску ввода в качестве номера телефона (000-000-0000) на QLabel при нажатии кнопок.

Например, если 2, затем 3, затем 2, затем 25, затем нажмите 8, QLabel будет обновляться как 232-58 и т. Д.

1 Ответ

1 голос
/ 07 мая 2019

Вы можете создать подкласс QLabel, который будет выполнять форматирование текста.

Например:

В заголовке:

class FormattedQLabel : public QLabel
{
    Q_OBJECT

    protected:
        int cursor_position;
        QString formatted_data;

    public:
        FormattedQLabel(QWidget * parent = nullptr);

    public slots:
        void update_data();
};

В источнике:

FormattedQLabel::FormattedQLabel(QWidget * parent) : QLabel(parent), cursor_position(0), formatted_data("000-000-0000")
{
    this->setText(formatted_data);
}

void FormattedQLabel::update_data()
{
    QPushButton * qpb = qobject_cast<QPushButton*>(sender());
    if(qpb != nullptr)
    {
        // handle the dash position
        if((cursor_position == 3) || (cursor_position == 7))
            ++cursor_position;

        // update the displayed value
        this->setText(this->text().replace(cursor_position, 1, qpb->text()));
        ++cursor_position;

        // if we exceed the text size, go back to the beginning (you can do something else)
        if(cursor_position == 12)
            cursor_position = 0;
    }
}

А затем подключите каждую QPushButton к этому слоту.


Полный пример кода

.h

class FormattedQLabel : public QLabel
{
    Q_OBJECT

    protected:
        int cursor_position;
        QString formatted_data;

    public:
        FormattedQLabel(QWidget * parent = nullptr);

    public slots:
        void update_data();
};

class Display : public QMainWindow
{
    Q_OBJECT

    protected:
        QPushButton * zero;
        QPushButton * one;
        QPushButton * two;
        QPushButton * three;
        QPushButton * four;
        QPushButton * five;
        QPushButton * six;
        QPushButton * seven;
        QPushButton * eight;
        QPushButton * nine;
        FormattedQLabel * disp;
        QPushButton * quit;

    public:
        Display();
};

.cpp

// --- FormattedQLabel ---
FormattedQLabel::FormattedQLabel(QWidget * parent) : QLabel(parent), cursor_position(0), formatted_data("000-000-0000")
{
    this->setText(formatted_data);
}

void FormattedQLabel::update_data()
{
    QPushButton * qpb = qobject_cast<QPushButton*>(sender());
    if(qpb != nullptr)
    {
        // handle the dash position
        if((cursor_position == 3) || (cursor_position == 7))
            ++cursor_position;

        // update the displayed value
        this->setText(this->text().replace(cursor_position, 1, qpb->text()));
        ++cursor_position;

        // if we exceed the text size, go back to the beginning (you can do something else)
        if(cursor_position == 12)
            cursor_position = 0;
    }
}

// --- Display ---
Display::Display()
{
    this->setWindowTitle("Test display");

    QWidget * central_area = new QWidget;
    this->setCentralWidget(central_area);

    QVBoxLayout * central_layout = new QVBoxLayout;
    disp = new FormattedQLabel(this);
    QGridLayout * keys_layout = new QGridLayout;
    zero =  new QPushButton("0", this);
    one =   new QPushButton("1", this);
    two =   new QPushButton("2", this);
    three = new QPushButton("3", this);
    four =  new QPushButton("4", this);
    five =  new QPushButton("5", this);
    six =   new QPushButton("6", this);
    seven = new QPushButton("7", this);
    eight = new QPushButton("8", this);
    nine =  new QPushButton("9", this);

    keys_layout->addWidget(seven, 0, 0);
    keys_layout->addWidget(eight, 0, 1);
    keys_layout->addWidget(nine,  0, 2);
    keys_layout->addWidget(four,  1, 0);
    keys_layout->addWidget(five,  1, 1);
    keys_layout->addWidget(six,   1, 2);
    keys_layout->addWidget(one,   2, 0);
    keys_layout->addWidget(two,   2, 1);
    keys_layout->addWidget(three, 2, 2);
    keys_layout->addWidget(zero,  3, 1);

    quit = new QPushButton("Exit", this);

    central_layout->addWidget(disp);
    central_layout->addLayout(keys_layout);
    central_layout->addWidget(quit);

    central_area->setLayout(central_layout);

    connect(zero, &QPushButton::clicked, disp, &FormattedQLabel::update_data);
    connect(one, &QPushButton::clicked, disp, &FormattedQLabel::update_data);
    connect(two, &QPushButton::clicked, disp, &FormattedQLabel::update_data);
    connect(three, &QPushButton::clicked, disp, &FormattedQLabel::update_data);
    connect(four, &QPushButton::clicked, disp, &FormattedQLabel::update_data);
    connect(five, &QPushButton::clicked, disp, &FormattedQLabel::update_data);
    connect(six, &QPushButton::clicked, disp, &FormattedQLabel::update_data);
    connect(seven, &QPushButton::clicked, disp, &FormattedQLabel::update_data);
    connect(eight, &QPushButton::clicked, disp, &FormattedQLabel::update_data);
    connect(nine, &QPushButton::clicked, disp, &FormattedQLabel::update_data);

    connect(quit, &QPushButton::clicked, qApp, &QApplication::quit);
}

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...