Qt 5
QTabWidget
count
返回tab的数量。返回值:int。计数从1开始。
QString
格式化字符串
arg
“%1, %2”.arg(1, 2)
QImage
cv::Mat to QImage
#include <QtGui/QImage>
// 或者 #include <QImage>
Mat mat = imread(path);
mat = cvtColor(mat, mat, cv::COLOR_BGR2RGB);
QImage qImage(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
QImage to Qpixmap
QPixmap pixMap = QPixmap::fromImaeg(qImage);
显示图像
lable 显示
ui.lable_image->setPixmap(pixMap);
ui.lable_image->setScaledContents(true);
ui.label_image->show();
label 缩放显示
pixMap->scaled(ui.label_Mat->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
字体
设置中文台字体
//先设置英文字体,再设置英文字体
QFont font;
font_.setFamilies({ QString("TimesNewRoman"),QString("宋体") });
设置整个软件字体
QApplication::setFont(font);
TreeWidget
使用TreeItem继承来确定树的分支关系;
按实例化的顺序确定显示顺序。
SpinBox
输入完全后才响应:setKeyboardTracking(false)
注册Qt未定义类型
qRegisterMetaType<newType>("newType");
QMessageBox
模态窗口
模态窗口显示在界面最顶层,不能操作其他界面。
QMessageBox *msg_box = new QMessageBox(this);
msg_box->exec();
非模态窗口
QMessageBox *msg_box = new QMessageBox;
msg_box->show();
固定大小
msg_box->setStyleSheet("QLabel{"
"min-width: 360px;"
"min-height: 200px; "
"font-size: 20px;"
"}");
弹窗后按键将失去响应,暂时还未找到方法
QSpinBox || QDoubleSpinbox
按键输入完整后响应:setKeyboardTracking(false)
UI操作都只能在主线程
重新connect QDoubleSpinBox/ QSpinBox
QObject::connect(gui_->spinBox_runSpeedHigh, QOverload<int>::of(&QSpinBox::valueChanged), this, &MotionControl::spinBox_runSpeedHigh_valueChanged);
QObject::connect(gui_->doubleSpinBox_initZ1, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &Board::doubleSpinBox_initZ1_valueChanged);
中文读取与写入
gui_->pushButton_estop->setText(QString::fromLocal8Bit("急停中"));
QString text = ui->lineEdit_->text().toLocal8Bit();
检查当前线程是否与主线程相同
#include <QApplication>
#include <QThread>
#include <QDebug>
void checkIfMainThread() {
if (QThread::currentThread() == QApplication::instance()->thread()) {
qDebug() << "This is the main thread.";
} else {
qDebug() << "This is NOT the main thread.";
}
}