【QT】QString的使用小练习

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_41374099/article/details/100908620

前言

QT可是个好东西,只怪自己太懒,多好的东西自己也只看到那新鲜。复习一下吧(其实还没怎么开始学呢)

这次做的例子是《QT5.9 C++开发指南》的samp4_2的修改,这个还挺简单打算做一下。问题是我现在的QT在控制台上还是不能正确输出中文,,,,,等这篇写完的时候不知道能不能找到解决这个问题的方法。

QT源码中输入中文

目前我找到解决方案了,这个网址给我了很大的帮助。我用的是VS2017编译的QT,所以第一个解决方案就让我解决了输不了中文的问题——在pro文件里添加这么几行

win32-msvc*: {
    QMAKE_CFLAGS *= /utf-8
    QMAKE_CXXFLAGS *= /utf-8
}

打开项目只有pro?

有的时候打开QT项目打开 .pro文件但是除了项目文件夹和 .pro 文件外缺少其他东西,这时候需要重新配置一下 Kits 就行了——工具 \rightarrow 选项 \rightarrow Kits,然后选择一个
套件Ok就行了。
   \;

UI设计

打开QT建一个Qt Widgets Application,类QWidget的项目,设计ui成下面这个样子

在这里插入图片描述

原例子中在这些控件下面放着几个Layout做对齐控制,比如上面一块区域就应该放一个Vertical Layout块。但是真要放Layout的话就很有问题,因为控件只能放在Layout上面,但是放上去后,控件又会被自动对齐,不好修改——比如一方上去,位置就设定在中间了,如果放入的是输入的长条框,那么立马就好被设置好大小(拉长)并对齐。

所以,目前我的解决方法是用Scroll Area来代替!!!
   \;

我的控件命名规则

学QT常常会看到许多控件的简写,但是这样还要猜这个东西本来是什么按钮、标签什么的,我选择不简写控件名。

比如一个pushButton 、一个label_2,我就会写成 XXX_pushButtonXXX_label2
不简写后面的名字,只在前面加一个功能说明。这样写的前提是你不担心变量、函数长度太长。
   \;

Ui_Widget类中没有足够的控件变量?

在这里插入图片描述
虽然把控件都放好了,但是ui_widget.h这个文件里还是只有几个控件变量,保存重启试试,一定要所有的控件变量都出现才行。

右键 setupUi() \rightarrow follow Symbol Undercursor就能打开这个头文件

   \;

关联

PushButton按钮右击,选择Go to slot/转向槽,选择clicked之后OK.

在这里插入图片描述

在程序运行时会被自动调用,这时的调用是类 Ui_Widget 内部执行的,之前的文章说过,这个Ui_XXX类是自动生成的,不用自己修改。

当点击这个按钮,就会由ui->append_pushButton发送一个clicked() 信号给 类Widget ,然后自动调用其槽函数on_append_pushButton_clicked()

//widget.h
private slots:
    void on_append_pushButton_clicked();

//widget.cpp
void Widget::on_append_pushButton_clicked()
{
	
}

这叫做QT的 signal/slots机制。一个类是否可以使用这个机制,需要看其类开头是否有 Q_OBJECT (不是自动创建的时候就需要自己加上这个宏)
   \;

QString字符串的操控

把上面18个按钮都设置一遍后,就有了18个空函数,分别写上18个对QString字符串操控的方法。

//widget.h
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_append_pushButton_clicked()
{
    //用str1来接收str1_comboBox的当前文本内容
    QString str1=ui->str1_lineEdit->text();
    QString str2=ui->str2_lineEdit->text();
    //把str2接在str1后面
    str1.append(str2);
    //在result上输出str1
    ui->result_lineEdit->setText(str1);

}

void Widget::on_prepend_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    QString str2=ui->str2_lineEdit->text();
    //把str2接在str1前面
    str1.prepend(str2);
    ui->result_lineEdit->setText(str1);

}

void Widget::on_toUpper_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    //str2是str1大写
    QString str2=str1.toUpper();
    //把str2输出到result上
    ui->result_lineEdit->setText(str2);
}

void Widget::on_toLower_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    //str2是str1小写
    QString str2=str1.toLower();
    //把str2输出到result上
    ui->result_lineEdit->setText(str2);
}

void Widget::on_left_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    //获取result_spinBox的大小
    int var=ui->result_spinBox->value();
    //取str1左边var个字符
    QString str2=str1.left(var);
    ui->result_lineEdit->setText(str2);
}

void Widget::on_right_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    int var=ui->result_spinBox->value();
    //取str1右边var个字符
    QString str2=str1.right(var);
    ui->result_lineEdit->setText(str2);
}

void Widget::on_trimmed_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    //去除首尾空格
    str1=str1.trimmed();
    ui->result_lineEdit->setText(str1);
}

void Widget::on_simplified_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    //去除首尾空格,也把中间连续的空格用一个空格代替
    str1=str1.simplified();
    ui->result_lineEdit->setText(str1);
}

void Widget::on_section_pushButton_clicked()
{

    QString str1=ui->str1_lineEdit->text();
    QString str2=ui->str2_lineEdit->text();
    int pos=ui->result_spinBox->value();


    QString str3;
    //Qt::CaseSensitive区分大小写
    //Qt::CaseInsensitive不区分大小写
    if(QString::compare(str2,"\\",Qt::CaseInsensitive)==0){    //如果str2是反斜杠 "\"
        str3=str1.section("\\",pos,pos+1);
        //以反斜杠作为分隔符,从第pos个部分,到第pos+1个部分
    }
    else{
        str3=str1.section(str2,pos,pos+1);////以str2作为分隔符,从第pos个部分,到第pos+1个部分
    }


    ui->result_lineEdit->setText(str3);
}

void Widget::on_count_pushButton_clicked()
{

    QString str1=ui->str1_lineEdit->text();
    //计数str1中字符的个数
    int num=str1.count();
    ui->result_spinBox->setValue(num);
    //result_lable是标签"结果"
    ui->result_label2->setText("count");
}

void Widget::on_size_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    //计数str1中字符的个数
    int num=str1.size();
    ui->result_spinBox->setValue(num);
    ui->result_label2->setText("size");
}

void Widget::on_indexOf_pushButton_clicked()
{


    QString str1=ui->str1_lineEdit->text();
    QString str2=ui->str2_lineEdit->text();
    //从左往右数,第一个满足条件的字符串所在的位置
    int pos=str1.indexOf(str2);
    ui->result_spinBox->setValue(pos);
    ui->result_label2->setText("indexOf");

}

void Widget::on_lastIndexOf_pushButton_clicked()
{

    QString str1=ui->str1_lineEdit->text();
    QString str2=ui->str2_lineEdit->text();
    //从左往右数,最后一个满足条件的字符串所在的位置
    int pos=str1.lastIndexOf(str2);
    ui->result_spinBox->setValue(pos);
    ui->result_label2->setText("lastIndexOf");

}

void Widget::on_endsWidth_pushButton_clicked()
{

    QString str1=ui->str1_lineEdit->text();
    QString str2=ui->str2_lineEdit->text();
    //看看str1是否是以str2结尾的
    bool n=str1.endsWith(str2);
    ui->result_checkBox->setChecked(n);
    ui->result_checkBox->setText("endsWith");

}

void Widget::on_startsWidth_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    QString str2=ui->str2_lineEdit->text();
    //看看str1是否是以str2开头的
    bool n=str1.startsWith(str2);
    ui->result_checkBox->setChecked(n);
    ui->result_checkBox->setText("startsWith");

}

void Widget::on_isNull_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    //isNull检测空字符串为false,检测未赋值的字符串为true
    //所以isNull可以用来判断字符串是否没有赋值
    bool n=str1.isNull();
    //是否是未赋值的字符串
    ui->result_checkBox->setChecked(n);
    //显示是isNull而不是isEmpty
    ui->result_checkBox->setText("isNull");

}

void Widget::on_isEmpty_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    //isEmpty检测空字符串或者未赋值的字符串都是true
    bool n=str1.isEmpty();
    //是否是空字符串或者未赋值字符串
    ui->result_checkBox->setChecked(n);

    //显示是isEmpty而不是isNull
    ui->result_checkBox->setText("isEmpty");

}

void Widget::on_contains_pushButton_clicked()
{
    QString str1=ui->str1_lineEdit->text();
    QString str2=ui->str2_lineEdit->text();
    //看看str1中是否含有str2
    bool n=str1.contains(str2);

    ui->result_checkBox->setChecked(n);
    ui->result_checkBox->setText("contains");

}

Layout

之前不知道那个布局Layout那么难弄有什么必要,今天终于搞清楚了。在进行窗口变换的时候,比如放大,缩小,或是Dock和UnDock的时候,布局就很重要了。为了让控件始终处在正确的比例处,需要用布局。

布局这东西的使用不是把其放在窗口内后,再放其他控件,而是把其他控件放好后,右击窗口空白处,选择布局,选择其中一种布局,比如栅格布局。这样随便更改大小,各控件比例都会正常了。

但是这也有其他问题,布局是占空间的!
在这里插入图片描述

这样的空隙显然不是我想要的,所以解决方法是找到那个Layout,选中后会在右下角的Filter里的Layout一栏看到
layoutLeftMargin                        \;\;\;\;\;\;\;\;\;\;\; 9
layoutTopMargin                        \;\;\;\;\;\;\;\;\;\;\; 9
layoutRightMargin                    \;\;\;\;\;\;\;\;\; 9
layoutBottomMargin                \;\;\;\;\;\;\; 9
layoutHorizontalSpacing    \; 6
layoutVerticalSpacing            \;\;\;\;\; 6
这样的属性,把这些都改成0就行了。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41374099/article/details/100908620