Qt practice record: Common Control Operation Example 1

QT common example of the operation of the control paper records. Comprising: QPushBotton, QLabel, QComboBox, QSlider, QSpinBox, edit boxes (QLineEdit / QPlainTextEdit / QTextEdit) and the like. To use as a guide, slowly add.

Common controls, there are many setting items are the same. If it is enabled / disabled, visible / invisible, set the prompt, and so on.

QPushBotton

Common operations:

使能、禁止:
    ui->pushButton->setEnabled(true);
    ui->pushButton->setEnabled(false);
可见/不可见:
    ui->pushButton->setVisible(true);
    ui->pushButton->setVisible(false);
提示语:
    ui->pushButton->setToolTip("button tips");
按钮扁平:
    ui->pushButton->setFlat(true);
设置文字、图标等:
    ui->pushButton->setText(tr("退出程序"));
    ui->pushButton->setIconSize(ui->pushButton->rect().size()); // 设置大小
    ui->pushButton->setIcon(QIcon(":images/exit.png"));
设置字体或大小:
    QFont qFont;
    qFont.setBold(true); // 这里设置加粗,其它默认
    ui->pushButton->setFont(qFont);
    ui->pushButton->setText(tr("监听端口"));
清除文字:
    ui->pushButton->clear();

Click event:

控件名称为 pushButton。  
private slots:
    void on_pushButton_clicked();

void MainWindow::on_pushButton_clicked()
{
    // ...
}

QLabel

设置图片:
    QPixmap pixmap(":images/logo.png");
    ui->label->setPixmap(pixmap);
    ui->label->setFixedSize(128, 128);
    ui->label->setScaledContents(true);

QComboBox

添加数据项:
    ui->comboBox->addItem("0"); // 单项添加
    QStringList list;
    list.clear();
    list << "1200" << "2400" << "4800" << "9600" << "14400" << \
         "19200" << "38400" << "43000" << "57600" << "76800" << \
         "115200" << "230400" << "256000" << "460800" << "921600";
    ui->comboBox->addItems(list); // 多项添加
设置当前项:
    ui->comboBox->setCurrentText(tr("115200"));
获取当前项:
    ui->comboBox->currentText();
    

QSlider

Horizontal Slider and Vertical Slider similar.

设置范围及当前值:
     ui->horizontalSlider->setRange(0, 100);
     ui->horizontalSlider->setValue(30);
     
获取值:
int val = ui->horizontalSlider->value();

数据变化事件
private slots:
    void on_horizontalSlider_valueChanged(int value);

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    showDebugInfo(value);
}

QSpinBox

设置范围及当前值:
     ui->spinBox->setRange(0, 100);
     ui->spinBox->setValue(30);
     
获取值:
int val = ui->spinBox->value();

数据变化事件
private slots:
    void on_spinBox_valueChanged(int value);

void MainWindow::on_spinBox_valueChanged(int arg1)
{
    showDebugInfo(arg1);
}

Text Box

This section of the text box to a certain extent can be considered a various types of controls, but is actually irrelevant, which QTextEdit and QPlainEdit inherited from QAbstractScrollArea, while QLineEdit is inherited from QWidget. Note that the first two eventually inherit QWidget, but QLineEdit direct successor to the QWidget.

QLineEdit

QLineEdit for single-line text display and input. Such as account numbers, passwords, and so on.

    ui->lineEdit->setPlaceholderText("username"); // 占位字符串,用于提示语
    ui->lineEdit->setText("lineEdit"); // 文字
    ui->lineEdit->setReadOnly(true); // 只读
    ui->lineEdit->setAlignment(Qt::AlignHCenter); // 居中对齐
    ui->lineEdit->setMaxLength(6); // 文字最大长度
    // QLineEdit::Password 文字用圆点替换,
    // QLineEdit::PasswordEchoOnEdit 输入时显示,结束后圆点替换
    // QLineEdit::NoEcho // 不显示任何内容,用于长度保护
    ui->lineEdit->setEchoMode(QLineEdit::Password);

Events (control named lineEdit):

on_lineEdit_textChanged:输入文字过程中响应
on_lineEdit_editingFinished:文字输入结束后响应(如按回车或Tab键)

QPlainTextEdit

For plain text input and display.

    ui->plainTextEdit->setPlaceholderText("sth text here");
    ui->plainTextEdit->setPlainText("foo");
    ui->plainTextEdit->setReadOnly(true);
    // 追加,分别支持html和纯文本
    ui->plainTextEdit->appendHtml("<font color=\"red\"> red </font>");
    ui->plainTextEdit->appendPlainText("add");

QTextEdit

For plain text, rich text input and display.

    ui->textEdit->setPlaceholderText("sth text here");
    ui->textEdit->setPlainText("foo"); // !! 也有setPlainText函数
    ui->textEdit->setText("foo");
    ui->textEdit->setReadOnly(true);
    ui->textEdit->append("<font color=\"red\"> red </font>"); // 追加,支持html

QPlainTextEdit and QTextEdit have textChangedincident response, but no editingFinishedincidents. Both support HTML syntax, for some applications may improve friendliness, such as the use of different types of text displayed in different colors, bold, italic, and so on.

QTextBrower

Inherited from QTextEdit, read-only mode, add navigation functions.

    ui->textBrowser->setHtml("a<br>b"); // 
    ui->textBrowser->setText("hello world\n");
    ui->textBrowser->append("a<br>b");

Lee Chi 2020.2.1 cloudy early eight, the situation is still grim, the village loudspeakers broadcast frequencies to increase the number of

发布了481 篇原创文章 · 获赞 244 · 访问量 110万+

Guess you like

Origin blog.csdn.net/subfate/article/details/104133129