我了解QLineEdit使用setInputMask
进行格式化。我想知道QLabel是否有类似的东西?
单击按钮时,我试图在QLabel上将输入掩码设置为电话号码(000-000-0000)。
例如,如果单击2然后3然后2然后5然后8,QLabel将更新为232-58,依此类推。
答案 0 :(得分:1)
您可以创建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);
}
我希望它能提供帮助。