Qt设置QLineEdit占位符文本字体和颜色

1.设置占位符文本

setPlaceholderText(const QString &);

2.MyLienEdit.h  

UI界面拖放一个QLineEdit,直接提升为这个类。 

#ifndef MYLINEEDIT_H
#define MYLINEEDIT_H

#include 
#include 

class MyLineEdit : public QLineEdit
{
    Q_OBJECT
public:
    explicit MyLineEdit(QWidget *parent = nullptr);

signals:

public slots:
	void slotLineEditChanged(const QString &text);

};

#endif // MYLINEEDIT_H

3.MyLienEdit.cpp 

#include "MyLineEdit.h"

#define ToStr(x) #x

const QString normalStyle = ToStr(font-family:"微软雅黑"; font-size: 14px; color: rgb(255,0,0););

const QString InStyle = ToStr(font-family:"微软雅黑"; font-size: 15px; color: rgb(0, 0, 255););

MyLineEdit::MyLineEdit(QWidget *parent)
    : QLineEdit(parent)
{
	this->setStyleSheet(normalStyle);
	connect(this, &QLineEdit::textChanged, this, &MyLineEdit::slotLineEditChanged);
}

void MyLineEdit::slotLineEditChanged(const QString &text)
{
	if (!this->text().isEmpty())
	{
		this->setStyleSheet(InStyle);
	}
	else
	{
		this->setStyleSheet(normalStyle);
	}
}

 

你可能感兴趣的:(Qt编程)