【Qt笔记】Qt学习笔记

《Qt学习笔记》

1 Qt-信号槽

Qt5开始信号槽有两种连接方式:

Qt5之前:方法一:connect(sender,SIGNAL(signal),receiver,SLOT(slot));

Qt5之后:方法二:connect(sender,&Sender::signal,receiver,&Receiver::slot);

对于方法二,是不需要声明槽函数为(private slots:)的,可以直接使用普通函数为槽函数。更为方便。

要注意,在Qt5之前一个信号连接多个槽,槽函数的执行顺序不是绝对的。但是Qt5中规定信号发射后,会按照槽的链接顺序执行。

2 Qt-窗口的渐渐出现和消失

在构造函数或者在需要的地方写下:

    //界面动画,改变透明度的方式出现0 - 1渐变

    QPropertyAnimation *animation = newQPropertyAnimation(this, "windowOpacity");

    animation->setDuration(5000);

    animation->setStartValue(0);

    animation->setEndValue(1);

    animation->start();

表示持续时间是5000ms,0-1表示出现;1-0表示消失;最后启动start()即可。

还可以连接槽函数,让QpropertyAnimation 结束后做其他的内容,如下:

    connect(animation, SIGNAL(finished()), this, SLOT(close()));

 

3 Qt-设置窗口背景

Qpalette来设置背景颜色(构造中)

方法一:

    QPalettepalette(this->palette());

    palette.setColor(QPalette::Background, /*Qt::red*/QColor(128, 128, 128));

    this->setPalette(palette);

    方法二:

    QPalettepalette;

    palette.setBrush(this->backgroundRole(), Qt::black);

    this->setPalette(palette);

 

QPixmap来设置背景图片(构造中)

    QPixmappixmap = QPixmap("Background.png").scaled(this->size()); 

//以上都是用scaled方式对图片进行了适应窗口大小的设置,因为所给的图片大小不一定满足要求,所以采用此方式!当然图片经过拉伸或者压缩之后会变形(纯色图片除外),所以对图片采用此方式时需要注意。

    QPalettepalette(this->palette());

    palette.setBrush(QPalette::Background, QBrush(pixmap));

    this->setPalette(palette);

 

4 Qt-透明提示框

可以实现类似于QQ登录错误提示框 关键代码    color.setAlphaF(0.6); //透明度0.6

InfoWidget::InfoWidget(QWidget *parent)

    :QWidget(parent)

{

    ui.setupUi(this);

    intwidth = this->width();

    this->resize(width, 28);

    //设置标题栏隐藏

    this->setWindowFlags(Qt::FramelessWindowHint);

    //设置背景色透明

    QPalettepalette;

    QColorcolor(190, 230, 250);

    color.setAlphaF(0.6); //透明度0.6

    palette.setBrush(this->backgroundRole(), color);

    this->setPalette(palette);

    //如果这个QWidget直接show,是有背景色的,但是如果放到一个父Widget中时,它就没有了效果。添加如下代码后就可以了:

    this->setAutoFillBackground(true);

    //构建关闭按钮

    close_button = newQToolButton(this);

    QPixmapclose_pix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);

    close_button->setIcon(close_pix);

    close_button->setStyleSheet("QToolButton{background-color: transparent;}");

    //获取主界面的宽度

    intheight = this->height();

    close_button->setGeometry(width - 20, 0, 20, 20);

    //设置提示图片

    msg_label = newQLabel(this);

    msg_label->setGeometry(QRect(5, 5, 20, 20));

    msg_label->setStyleSheet("background-color: transparent;");

    msg_label->setScaledContents(true);

    //设置提示信息

    ask_label = newQLabel(this);

    ask_label->setStyleSheet("background-color: transparent; color: red;");

    ask_label->setGeometry(QRect(30, 0, width - 60, height));

    ask_label->setAlignment(Qt::AlignCenter);

    close_button->setCursor(Qt::PointingHandCursor);

    //信号槽

    QObject::connect(close_button, SIGNAL(clicked()), this, SLOT(closeWidget()));

}

5 Qt-鼠标样式

使用setCursor(Qt::CursorShape)来设置鼠标样式,Qt::CursorShape包含以下选项

6 Qt-中文乱码

//获取系统编码,否则则会出现乱码

QtextCodec *codec = QtextCodec::codecForName(“System”);

//设置和对本地文件系统读写时候的默认编码格式

QtextCodec::setCodecForLocal(codec);

//设置传给tr函数时的默认字符串编码

QtextCodec::setCodecForTr(codec);

//用在字符串常量或者QbyteArray构造Qstring对象时使用的一种编码格式

QtextCodec::setCodecForCStrings(codec);

7 Qt-之获取本机网络信息

涉及到的类主要是:

QHostInfo
QHostAddress
QNetworkInterface
QnetworkAddressEntry

8 Qt-界面实现技巧

1、 窗口 最小化 最大化 关闭按钮、显示状态自定义
setWindowFlags(Qt::CustomizeWindowHint);
setWindowFlags(Qt::WindowCloseButtonHint);//只要关闭按钮
setWindowFlags(Qt::WindowFlagstype)
Qt::FrameWindowHint:没有边框的窗口
Qt::WindowStaysOnTopHint://总在最上面的窗口
Qt::CustomizeWindowHint://自定义窗口标题栏,以下标志必须与这个标志一起使用才有效,否则窗口将有
默认的标题栏
Qt::WindowTitleHint//显示窗口标题栏
Qt::WindowSystemMenuHint//显示系统菜单
Qt::WindowMinimizeButtonHint//显示最小化按钮
Qt::WindowMaximizeButtonHint//显示最大化按钮
Qt::WindowMinMaxButtonsHint//显示最小化按钮和最大化按钮
Qt::WindowCloseButtonHint//显示关闭按钮

2、 设置应用程序的字体
QFontfont("Courier", 10, QFont::Normal, false);
QApplication::setFont(font);

3、  

猜你喜欢

转载自blog.csdn.net/hellozex/article/details/80840676