QT--文字输入及背景设置

        

效果如图所示:

使用的QT版本为QT5.15.2

        代码:maindow.cpp

#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include<QString>
#include<QMessageBox>
#include<QKeyEvent>
#include<QEvent>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->pushButton->setFocus();
    ui->pushButton->setDefault(true);
    ui->lineEdit->installEventFilter(this);
    this->setStyleSheet("background-color: qlineargradient(spread:pad, "
                        "x1:0, y1:0, x2:1, y2:0, "
                        "stop:0 rgba(251,102,102, 200), "
                        "stop:1 rgba(20,196,188, 210));");
 
    ui->lineEdit->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                    "color: rgb(31,31,31);"		//字体颜色
                                    "padding-left:20px;"       //内边距-字体缩进
                                    "background-color: rgb(255, 255, 255);" //背景颜色
                                    "border:2px solid rgb(20,196,188);border-radius:15px;");//边框粗细-颜色-圆角设置
    ui->textBrowser->setStyleSheet("font: 25 14pt '微软雅黑 Light';" //字体
                                    "color: rgb(31,31,31);"		//字体颜色
                                    "padding-left:20px;"       //内边距-字体缩进
                                    "background-color: rgb(255, 255, 255);" //背景颜色
                                    "border:2px solid rgb(20,196,188);border-radius:15px;");//边框粗细-颜色-圆角设置
    ui->pushButton->setStyleSheet("QPushButton{font: 25 14pt '微软雅黑 Light';color: rgb(255,255,255);background-color: rgb(20,196,188);"
                                    "border: none;border-radius:12px;}"
                                    "QPushButton:hover{background-color: rgb(22,218,208);}"//hover
                                    "QPushButton:pressed{background-color: rgb(17,171,164);}");//pressed
    ui->pushButton_2->setStyleSheet("QPushButton{font: 25 14pt '微软雅黑 Light';color: rgb(255,255,255);background-color: rgb(20,196,188);"
                                    "border: none;border-radius:12px;}"
                                    "QPushButton:hover{background-color: rgb(22,218,208);}"//hover
                                    "QPushButton:pressed{background-color: rgb(17,171,164);}");//pressed
 
    //QLabel *label = new QLabel;
    ui->label->setAttribute(Qt::WA_TranslucentBackground);
 
    //ui->label->setStyleSheet("background:transparent");
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_pushButton_clicked()
{
    QString std = ui->lineEdit->text();
    QString std1 = std.trimmed();//去掉字符串的前面空格和后面的换行符
    if(std==NULL||std1==""){
        QMessageBox::warning(this,"警告","你所输入的信息为空");
        ui->lineEdit->clear();
    }else{
        ui->lineEdit->setFocus();
        ui->textBrowser->append(std);
        ui->lineEdit->clear();
    }
}
 
 
void MainWindow::on_pushButton_2_clicked()
{
    ui->textBrowser->clear();
}
 
bool MainWindow::eventFilter(QObject* target, QEvent *event)
{
    if(target == ui->lineEdit)		//可替换
            {
                if(event->type() == QEvent::KeyPress)//回车键
                {
                     QKeyEvent *k = static_cast<QKeyEvent *>(event);
 
                     if(k->key() == Qt::Key_Return)
                     {
                         on_pushButton_clicked();		//替换为需要响应的函数事件,以这里的按钮为例
                         return true;
                     }
                }
            }
            return QWidget::eventFilter(target,event);
 
}
代码:mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
 
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void on_pushButton_clicked();
 
    void on_pushButton_2_clicked();
private:
    Ui::MainWindow *ui;
protected:
    bool eventFilter(QObject *target, QEvent *event);//事件过滤器
};
#endif // MAINWINDOW_H
 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值