当前位置:网站首页>Qt日常学习
Qt日常学习
2022-08-04 05:29:00 【视觉菜鸟Leonardo】
2022/07/28
使用Visual Studio2019 搭配 Qt5.14,可视化Qt界面使用QtDesigner。
若只使用代码来做界面太麻烦,效率低;只使用可视化界面进行拖拽效率高但无法实现全部功能。使用代码+可视化结合的方式能够实现绝大部分功能。
练习1:将进度条添加到状态栏中,创建label组件添加到状态栏中,在状态栏中创建进度显示,效果如图:
.h文件:
private:
QLabel* fLabCurFile;//状态栏显示当前文件的Label
QProgressBar* progressBar1;//状态栏上的进度条
QSpinBox* spinFontSize;//字体大小
QFontComboBox* comboFont;//字体名称
void iniUI();//代码实现UI的初始化
.cpp文件:
void Qt0::iniUI() {
//状态栏上添加组件
fLabCurFile = new QLabel;
fLabCurFile->setMinimumWidth(150);
fLabCurFile->setText("当前文件:");
ui.statusBar->addWidget(fLabCurFile);//添加到状态栏
//progressBar1 = new QProgressBar;
progressBar1 = new QProgressBar;
progressBar1->setMaximumWidth(200);
progressBar1->setMinimum(5);
progressBar1->setMaximum(50);
progressBar1->setValue(ui.textEdit->font().pointSize());
ui.statusBar->addWidget(progressBar1);//添加到状态栏
//工具栏上添加组件
spinFontSize = new QSpinBox;
spinFontSize->setMinimum(5);
spinFontSize->setMaximum(50);
spinFontSize->setValue(ui.textEdit->font().pointSize());
spinFontSize->setMinimumWidth(50);
ui.mainToolBar->addWidget(new QLabel("字体大小 "));
ui.mainToolBar->addWidget(spinFontSize);//spinbox添加到工具栏
ui.mainToolBar->addSeparator();//分隔条
ui.mainToolBar->addWidget(new QLabel("字体 "));
comboFont = new QFontComboBox;
comboFont->setMinimumWidth(150);
ui.mainToolBar->addWidget(comboFont); //添加到工具栏
setCentralWidget(ui.textEdit);
//progressBar1-
}
//在构造函数中,调用函数,实现添加功能
Qt0::Qt0(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
iniUI();//调用添加函数
}
解决问题1:
QT显示界面中文乱码:
在头文件中添加如下代码:
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif
2022/7/29
对于visual studio中使用qt designer,不能直接使用右键->转到槽的方式设置槽函数,也不能使用
上述两种方式均不会将信号传递到槽中。
在vs中,使用下面这种方式更加有效:
其中&用来传递指针,槽函数正常编写。
代码:
Qt4::Qt4(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
iniSignalSlots();
}
void Qt4::iniSignalSlots() {
connect(ui.btnTotal, &QPushButton::clicked, this, &Qt4::on_btnCal_clicked);
}
void Qt4::on_btnCal_clicked() {
QString str = ui.editNums->toPlainText();//获取文本框中的内容
qDebug() << "str==" << str;
int num = str.toInt();
str = ui.editPrice->toPlainText();
float price = str.toFloat();
float total = num * price;
str = str.sprintf("%.2f", total);
ui.editPrices->setText(str);
}
2022/08/03
在工具栏中嵌入ToolButton(图标式),单纯的在Qtdesigner中无法直接拖动,需要配合代码。
首先在Qtdesigner中拖出N个ToolButton,对于每个TBtn赋予相应的Action:
在图标出选择相应的ico文件,至此完成Designer上的工作,下面需要在VS中创建相应的函数将其显示到工具栏中并编辑Action:
//弹出菜单
void Qt46::creatSelectionPopMenu() {
//创建下拉菜单
QMenu* menuSelection = new QMenu(this);//创建选择弹出式菜单
ui.mainToolBar->addWidget(ui.tbtnListIni);
ui.mainToolBar->addSeparator();//分隔条
ui.mainToolBar->addWidget(ui.tbtnListClear);
ui.mainToolBar->addSeparator();//分隔条
ui.mainToolBar->addWidget(ui.tbtnListInsert);
ui.mainToolBar->addSeparator();//分隔条
ui.mainToolBar->addWidget(ui.tbtnListAppend);
ui.mainToolBar->addSeparator();//分隔条
ui.mainToolBar->addWidget(ui.tbtnListDelete);
ui.mainToolBar->addSeparator();//分隔条
menuSelection->addAction(ui.actSelALL);
menuSelection->addAction(ui.actSelNone);
menuSelection->addAction(ui.actSelInvs);
}
边栏推荐
猜你喜欢
Attention Is All You Need(Transformer)
RecyclerView的用法
flink-sql自定义函数
[CV-Learning] Semantic Segmentation
[Deep Learning 21-Day Learning Challenge] 3. Use a self-made dataset - Convolutional Neural Network (CNN) Weather Recognition
Kubernetes基本入门-概念介绍(一)
Logistic Regression --- Introduction, API Introduction, Case: Cancer Classification Prediction, Classification Evaluation, and ROC Curve and AUC Metrics
[Introduction to go language] 12. Pointer
双重指针的使用
字典特征提取,文本特征提取。
随机推荐
安装dlib踩坑记录,报错:WARNING: pip is configured with locations that require TLS/SSL
(TensorFlow) - detailed explanation of tf.variable_scope and tf.name_scope
CAS与自旋锁、ABA问题
Androd Day02
flink-sql所有表格式format
Android connects to mysql database using okhttp
(十四)平衡二叉树
[Deep Learning 21 Days Learning Challenge] Memo: What does our neural network model look like? - detailed explanation of model.summary()
Jupyter Notebook安装库;ModuleNotFoundError: No module named ‘plotly‘解决方案。
SQL练习 2022/7/2
剑指 Offer 2022/7/2
SQL练习 2022/7/5
判断字符串是否有子字符串重复出现
TensorFlow2 study notes: 6. Overfitting and underfitting, and their mitigation solutions
SQL练习 2022/7/3
k9s-终端UI工具
[CV-Learning] Semantic Segmentation
Thoroughly understand box plot analysis
彻底搞懂箱形图分析
线性回归02---波士顿房价预测