qt使用记录

8个设计师必看的免费UI图标设计资源站_兮梦源的博客-CSDN博客

报错:QObject::startTimer: Timers cannot be started from another thread

对象包含QTimer,对象在主线程定义,而对象由被moveToThread到一个线程中,线程中connect这个QTimer报错。因此QTimer可以是个指针,在主线程定义对象的时候,QTimer没有new实例,在线程中new实例这个QTimer,然后在connect即可!

qt4.x截图:

QPixmap tmpPmap(QPixmap::grabWindow(QApplication::desktop()->winId()));
               // 保存截图
               tmpPmap.save("screenshot.jpg", "JPG");

qt5截图:

        QScreen *screen = QGuiApplication::primaryScreen();
        // 截取整个屏幕
        QPixmap originalPixmap = screen->grabWindow(0);
        // 保存截图
        originalPixmap.save("screenshot.png", "PNG");

很久以前用过qt,没记录,这次记录一下。这里试Ubuntu linux环境下操作。

QObject::startTimer: timers cannot be started from another thread

表示创建QTimer的线程和QTimer->start调用不在同一线程。new QTimer与QTimer->start放到同一线程即可。

交叉编译,我用的是qt-5.9.1,交叉编译工具是arm-linux-gnueabihf-g++:

qtbase/mkspecs/linux-arm-gnueabi-g++用来复制一份qtbase/mkspecs/linux-arm-gnueabihf-g++

编辑qtbase/mkspecs/linux-arm-gnueabihf-g++/qmake.conf

#
# qmake configuration for building with arm-linux-gnueabi-g++
#

MAKEFILE_GENERATOR      = UNIX
CONFIG                 += incremental
QMAKE_INCREMENTAL_STYLE = sublib

include(../common/linux.conf)
include(../common/gcc-base-unix.conf)
include(../common/g++-unix.conf)

# modifications to g++.conf
QMAKE_CC                = arm-linux-gnueabihf-gcc
QMAKE_CXX               = arm-linux-gnueabihf-g++
QMAKE_LINK              = arm-linux-gnueabihf-g++
QMAKE_LINK_SHLIB        = arm-linux-gnueabihf-g++

# modifications to linux.conf
QMAKE_AR                = arm-linux-gnueabihf-ar cqs
QMAKE_OBJCOPY           = arm-linux-gnueabihf-objcopy
QMAKE_NM                = arm-linux-gnueabihf-nm -P
QMAKE_STRIP             = arm-linux-gnueabihf-strip
load(qt_config)

添加编译脚本,其中tslib、sqlite3库事先编译好,头文件和库放到指定文件夹中,用-I,-L包含进去:

#!/bin/bash
cd qt-everywhere-opensource-src-5.9.1/
./configure \
-v \
-prefix /opt/armzero/qt-5.9.1 \
-release -c++std c++11 \
-opensource \
-confirm-license \
-no-accessibility \
-make libs \
-xplatform linux-arm-gnueabihf-g++ \
-linuxfb \
-optimized-qmake \
-pch \
-qt-libjpeg \
-qt-libpng \
-sql-sqlite \
-qt-zlib \
-tslib \
-shared \
-no-opengl \
-no-sse2 \
-no-openssl \
-no-iconv \
-no-cups \
-no-glib \
-no-pkg-config \
-recheck-all \
-no-separate-debug-info \
 -I /opt/armzero/armlib/include \
 -L /opt/armzero/armlib/lib
make -j4
sudo make install

1、安装qt creator。去官网下载:http://download.qt.io/archive/qt/

wget Index of /archive/qt/5.1/5.1.0qt-linux-opensource-5.1.0-x86-offline.run

视需求而定,然后chmod +x qt-linux-opensource-5.1.0-x86-offline.run一下,./qt-linux-opensource-5.1.0-x86-offline.run即可安装。

2、配置:我们还可能需要交叉编译环境做qt开发,参考如下:交叉编译环境需要下源代码,手动编译!

Linux下如何利用QtCreator编译ARM版本的Qt程序_小黄花一朵的博客-CSDN博客

3、如果我们用其他电脑编译的交叉编译qmake,拷贝过来用上面(2)的方法会出现:“qt没有被正确的安装,请运行make install”错误。我们可以用2进制方式打开qmake文件,找到:71 74 5f 70 72 66 78 70 61 74 68 3d,后面跟的是qmake安装路径,我们把拷贝来的库放到该路径即可!

参考:“qt没有被正确的安装,请运行make install”解决方法_阳光柠檬_的博客-CSDN博客

4、pc端编译:出现:-1: error: cannot find -lGL 错误

sudo apt-get install libgl1-mesa-dev

5、字体,QString text1 = QObject::tr("hello"); 国际化(翻译),按键,Qsetting配置文件,多页面,自动拉伸,QstackWidget堆叠串口,setIcon。

6、QState、QStateMachine设计状态机,参考Qt状态机框架_求道玉的博客-CSDN博客_qt 状态机

7、添加资源文件:比如翻译和icon文件,

在函数中可以直接调用,比如翻译:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTranslator tst;
    tst.load(":/translater/translators/tr_zh.qm");// 这里资源文件的配置目录是:translater;translators/tr_zh.qm是文件的路径。当然把需要的文件拷贝到系统,用系统路径(相对路径,执行文件同一路径:./translators/tr_zh.qm;或绝对路径)
    a.installTranslator(&tst);

    funcWork w;
    w.show();
    
    return a.exec();
}

qt中建立图片资源文件 - 沙漠里的孤鲸 - 博客园

8、国际化参考:

QT国际化(中英转换) - h2z - 博客园

9、资源管理,参数管理:

    QSettings myset(INI_FILE_PATH, QSettings::IniFormat);;
    for(groupParmType gpi : baseType::parms)
    {
        myset.beginGroup(gpi.groupName);
        for(parmType pj : gpi.grouplist)
        {
            if(!myset.contains(pj.parmName))
            {
                myset.setValue(pj.parmName,
                               pj.val);
            }
            else
            {
                QString getval = myset.value(pj.parmName).toString();
                coutdbg << "pname: " << pj.parmName.toStdString() << ",val=" << getval.toStdString() << std::endl;
            }
        }
        myset.endGroup();
    }

10.QObject对象如果想他的信号与槽在另外一个线程处理:

QThread *backthread = new QThread();

QObjectUsr *deal = new QObject();// 其中QObjectUsr继承了QObject

deal->moveToThread(backthread);

然后绑定信号与槽即可。

10.2:信号与槽参数类型和返回类型必须一致,槽用:private slots:标记,信号用signals:标记。

11.对一layout边缘问题

layoutLeftMargin,layoutTopMargin,layoutRightMargin,layoutBottomMargin用于配置layout的边缘宽度

12.qss。在ui设计界面中,右键对象,选择“改变样式表”也可以修改,但是qss更好一点。

        a:配置控件对象名字为frame1,和frame2的格式。color颜色,border边缘宽度,border-radius边缘幅度,padding边框宽度,background背景色,qlineargradient自定义色彩

#frame1, #frame2 {
color: red;
border: 1px solid lightgray;
border-radius: 6;
padding: 0px;    
background: #3399CC;

background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(51, 135, 204, 255), stop:0.176471 rgba(51, 222, 205, 255), stop:0.98 rgba(51, 135, 204, 255), stop:1 rgba(0, 0, 0, 0));

min-width: 40px;
min-height: 20px;       
}

     b:对控件QFrame对象,都进行配置

QFrame {
color:#CCCCCC;
}

在main函数中运行即可生效:

    QFile file(":/prefix/qss/my.qss");

    file.open(QFile::ReadOnly);
    QString myStyleSheet = QLatin1String(file.readAll());
    a.setStyleSheet(myStyleSheet);

参考:

QDialog {
background-color: #242A38;
border: 0px solid gray;
}

QToolButton {
background: transparent; 
border: 0px;
border-style:hidden
}

QPushButton {
background: lightgray;
border: 0px solid red;
color: black;
border-radius: 2px;
}

QPushButton:pressed{
color: white;
background-color: #2E3648;
}

QLabel {
background: transparent; 
color: white;
}

QLineEdit {
background: lightgray; 
color: black;
}

QLineEdit:focus {
color:rgb(200, 0, 0);
}

QComboBox {
background-color: #191F2C;
color: white;
border: 0px solid lightgray;
min-width: 100px;
min-height: 50px;
}

QComboBox:disabled {
color: gray;
}

QComboBox QAbstractItemView {
background: #495164;
color: #D2D1D7;
border-radius: 10px;
border: 1px solid gray;
selection-background-color: #96C8FA;
selection-color: black;
} 

QComboBox QAbstractItemView::item {
min-height: 60px;
}

QComboBox::drop-down {
width: 40px;
border-left: 1px solid black;
}

QComboBox::down-arrow {
image: url(:/prefix/image/icon_list_cbb_normal.png);
height:10px;
width:18px;
}  

QCheckBox {
color: white;
spacing: 10px;
}

QCheckBox::indicator {
width: 86px; 
height: 54px;
}

QCheckBox::indicator:unchecked {
image:url(:/prefix/image/btn_huakuai_default.png);
}
 
QCheckBox::indicator:checked {
image:url(:/prefix/image/btn_huakuai_pressed.png);
}

QRadioButton {
color: white;
}

QRadioButton::indicator {
width: 36px; 
height: 36px;
}

QRadioButton::indicator:unchecked {
image:url(:/prefix/image/rb_normal.png);
}
 
QRadioButton::indicator:checked {
image:url(:/prefix/image/rb_pressed.png);
}

QSpinBox::up-button {
width: 48px;
}

QSpinBox::down-button {
width: 48px;
}

QAbstractSpinBox::up-button {
width: 48px;
}

QAbstractSpinBox::down-button {
width: 48px;
}

QDateTimeEdit,QTimeEdit {
background: #191F2C;
color: white;
min-height: 60px;
}

QDateEdit {
background: #191F2C;
color: white;
min-height: 60px;
}

QDateEdit::drop-down {
image: url(:/prefix/image/icon_list_cbb_normal.png);
width: 48px;
height: 20px;
background-color: white;
border-style: solid;
border-width: 4px;
border-color: rgb(100,100,100);
spacing: 5px;
}

QPushButton
{
    border-image:url(:/prefix/image/rb_normal.png);
}
QPushButton:checked
{
    border-image:url(:/prefix/image/rb_pressed.png);
}

13:出现错误:Object::connect: No such signal 参考:【QT‘】 Object::connect: No such signal__helloWH_的博客-CSDN博客

去掉参数名, 保留类型即可

MyThread *Rev = new MyThread();
    connect(Rev, SIGNAL(sendData(char* ,int )), this, SLOT(SerialRevDataHandle(char *,int)));

signals:
    void SignalCommitIeDealResult(IMEXoptInformStruct cmd);
private slots:
    void HandleDealUsbIeCmd(IMEXoptInformStruct result);

14:出现乐:QObject::connect: Cannot queue arguments of type。参考:Qt出现QObject::connect: Cannot queue arguments of type '******'的解决方法 - eversliver - 博客园

    qRegisterMetaType<IMEXoptInformStruct>("IMEXoptInformStruct");

编译参考:

1. 下载qt-everywhere-opensource-src-4.8.7.tar.gz源码


2.
进入qt-everywhere-opensource-src-4.8.7/mkspecs/qws目录,新建linux-himix200-g++目录,复制linux-arm-g++目录下的所有文件到linux-himix200-g++

cp –a linux-arm-g++/* linux-himix200-g++

       2)进入linux-himix200-g++,修改文件qmake.conf

             

# modifications to g++.conf

QMAKE_CC               = arm-himix200-linux-gcc

QMAKE_CXX             = arm-himix200-linux-g++

QMAKE_LINK            = arm-himix200-linux-g++

QMAKE_LINK_SHLIB      = arm-himix200-linux-g++

# modifications to linux.conf

QMAKE_AR                = arm-himix200-linux-ar cqs

QMAKE_OBJCOPY          = arm-himix200-linux-objcopy

QMAKE_STRIP             = arm-himix200-linux-strip


3.配置


./configure --prefix=/home/haosong/work/temp-disk/hi3516-qt4.8.7 \

-opensource -confirm-license -qt-sql-sqlite \

-qt-gfx-linuxfb -plugin-sql-sqlit -no-qt3support \

-no-phonon -no-svg -no-webkit -no-javascript-jit \

-no-script -no-scripttools -no-declarative -no-declarative-debug \

-qt-zlib -no-gif -qt-libtiff -qt-libpng -no-libmng -qt-libjpeg \

-no-rpath -no-pch -no-3dnow -no-avx -no-neon -no-openssl \

-no-nis -no-cups -no-dbus -embedded arm \

-platform linux-g++ -xplatform qws/linux-himix200-g++ \

-little-endian -qt-freetype -no-opengl -no-glib -nomake demos \

-nomake examples -nomake docs -nomake tools
修改 itemviews.cpp 396 行,解决编译错误: error: invalid conversion frome 'int' to

view()->selectionModel()->select(index, QItemSelectionModel::SelectionFlags( QItemSelectionModel::Columns & QItemSelectionModel::Deselect ));
4.修改src/3rdparty/sqlite.pri

DEFINES += SQLITE_OMIT_LOAD_EXTENSION SQLITE_OMIT_COMPLETE SQLITE_THREADSAFE=2
增加SQLITE_THREADSAFE=2, 即表示sqlite支持多线程模式。在特定条件下可保证线程安全。这个特定条件就是,每个线程独立打开数据库,在本线程内使用

自己的数据库连接。

关于二维码:开发环境 Ubuntu18.04 + qt 5.9.3 +libqrencode + QZXing

https://www.freesion.com/article/8854312189/

生成验证码:Qt,C++实现图片验证码_荒野山人的博客-CSDN博客_c++ 生成验证码图片

mainwindow布局:

Qt MainWindow窗口部件简介-CSDN博客

空间分布:

在CSS中,盒模型中的padding、border、margin是什么意思?(内容、内边距、边框、外边距)_padding和border-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值