Qt 中常用功能

设置应用程序图标
在资源文件(*.res)中添加图标文件
资源文件代码如下:

bmp/logo.png

在setupUi函数中添加相应代码
代码如下:

//set icon
QIcon icon;
icon.addPixmap(QPixmap(QString::fromUtf8(":/bmp/logo.png")),QIcon::Normal,
QIcon::Off);
WndTest->setWindowIcon(icon);
WndTest->setIconSize(QSize(256, 256));

在程序中显示图片(QLabel)

在程序的相应位置中添加相应代码
代码如下:

QLabel *lLogo;

lLogo = new QLabel();
lLogo->setObjectName(QString::fromUtf8(“lLogo”));
lLogo->setGeometry(QRect(160, 110, 128, 128));
lLogo->setPixmap(QPixmap(QString::fromUtf8(":/bmp/logo.png")));
lLogo->setScaledContents(true);

字体更改

QFont font;
font.setPointSize(40);
font.setBold(true);
font.setWeight(75);
QLabel *lfontnew = new QLabel();
lfontnew->setFont(font);

获取屏幕或工作区大小

#include

//获取屏幕分辨率第一种方法
qDebug()<< “screen width:”<QApplication::desktop()-width();
qDebug()<< “screen height:”<QApplication::desktop()-height();
//获取屏幕分辨率第二种方法
qDebug()<< “screen width:”<desktop()->width();
qDebug()<< “screen height:”<desktop()->height();

//获取客户使用区大小
qDebug()<<“screen avaliabe width:”
<QApplication::desktop()-availableGeometry().width();
qDebug()<<“screen avaliabe heigth:”
<QApplication::desktop()-availableGeometry().height();

//获取应用程序矩形大小
qDebug()<< “application avaliabe width:”
<QApplication::desktop()-screenGeometry().width();
qDebug()<< “application avaliabe heigth:”
<QApplication::desktop()-screenGeometry().height();

文本颜色更改

void WndTest::changeColor( QWidget *obj, QColor clr )
{
QPalette *palette = new QPalette();
palette->setColor(QPalette::Text,clr);
obj->setPalette(*palette);
delete palette;
}

调用:
changeColor( leid, Qt::blue );
//或
changeColor(leid,QColor::fromRgb(240,240,240));

日期与时间转换成QString

//“yyyyMMdd"为转换格式,该格式转换后日期如"2020415”
QString date_str = QDate::currentDate().toString(QString(“yyyyMMdd”));
//“hhmmss"为转换格式,该格式转换后时间如"080359”
QString time_str = QTime::currentTime().toString(QString(“hhmmss”));

猜你喜欢

转载自blog.csdn.net/weixin_43778462/article/details/105819610