QT学习小结(一)

学习使用QT有段时间了,初步先做下小结,留作备忘。


目录


1. 获取屏幕或工作区大小
2. 设置应用程序图标
3. 在程序中显示图片(QLabel)
4. 字体更改
5. 文本颜色更改
6. 日期与时间转换成QString


==========================================================================
1. 获取屏幕或工作区大小


#include <QDesktopWidget>


    //获取屏幕分辨率
    qDebug()<< "screen width:"<<QApplication::desktop()->width();
    qDebug()<< "screen height:"<<QApplication::desktop()->height();
    //下面方法也可以
    qDebug()<< "screen width:"<<qApp->desktop()->width();
    qDebug()<< "screen height:"<<qApp->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();


<<返回目录
---------------------------------------------------------------------------------------------


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


<RCC>
<qresource prefix="/" >
<file>bmp/logo.png</file>
</qresource>
</RCC>


b. 在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));


<<返回目录
---------------------------------------------------------------------------------------------


3. 在程序中显示图片(QLabel)
a. 在资源文件(*.res)中添加图片文件(方法同2.a)
b. 在程序的相应位置中添加相应代码
代码如下:


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);


<<返回目录
---------------------------------------------------------------------------------------------


4. 字体更改


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


<<返回目录
---------------------------------------------------------------------------------------------


5. 文本颜色更改


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));


<<返回目录
---------------------------------------------------------------------------------------------


6. 日期与时间转换成QString


QString date_str = QDate::currentDate().toString(QString("yyyyMMdd")); //"yyyyMMdd"为转换格式,该格式转换后日期如"20121205",还有更多格式,请查看帮助
QString time_str = QTime::currentTime().toString(QString("hhmmss")); //"hhmmss"为转换格式,该格式转换后时间如"080359",还有更多格式,请查看帮助
posted @ 2015-03-10 10:55  SunkingYang  阅读(411)  评论(0编辑  收藏  举报