C++/Qt 开发过程中的一些技巧性总结

一.C++

1.变量的修饰

  • auto:属于一次性存储,其存储空间可被若干变量重复覆盖使用
  • register:存放在通用寄存器中
  • extern:在所有函数和程序段中都可以引用
  • static:在内存中时以固定地址存放的,在整个程序运行期间都有效

2.显示转换操作符

  • static_case
  • dynameic_cast
  • const_cast
  • reinterpret_cast

3.bool的默认值

  • true=1
  • false=0

4.进制输出

    //进制输出
    int a = 1000;
    cout << "十进制:"<< dec << a << endl;
    cout << "八进制:" << oct << a << endl;
    cout << "十六进制:" << hex << a << endl;
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

5.九九乘法表

    //九九乘法表
    for (int i = 1; i <= 9; i++)
    {
        for (int j = 1;j<=i;j++)
        {
            cout << i << " * " << j << " = " << i * j << " ";
        }
        cout << endl;
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

6.预处理命令

#define
#error
#if
#else
#elif
#endif  
#ifdef
#ifndef
#undef
#line
#pragma
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

7.数组长度

C++并没有直接提供数组的长度方法,我们可以这样计算

int main()
{
    int arr[] = { 1,5,9,10,9,2 };

    //方法一
    cout << "数组的长度:" << (end(arr) - begin(arr))<< endl;
    //方法二
    cout << "数组的长度:" << (sizeof(arr) / sizeof(arr[0])) << endl;

    system("pause");
    return 0;
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

二.QT

1.LineEdit

  • 1.输入密码
this->ui->lineEdit->setEchoMode(QLineEdit::Password);
  
  
  • 1
  • 2.智能提示
    //可以设置匹配模式
    QStringList list;
    list << "1" << "12" << "123"<< "1234"<< "12345";
    QCompleter * completer = new QCompleter(list,this);
    this->ui->lineEdit->setCompleter(completer);
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

这里写图片描述

2.控件移动

//动态移动
setGeometry(30,30,3,0)
  
  
  • 1
  • 2

3.启动程序

QProcess *myProcess = new QProcess();
//获取到输入框输入的内容启动,类似cmd
myProcess->start(this->ui->cmd_line->text().trimmed());
  
  
  • 1
  • 2
  • 3

4.Notepad

最近做记事本积累下来的小技巧

  • 1.打开文件
void MainWindow::on_action_O_triggered()
{
    //打开窗口获取到文件绝对路径
    QString filePath = QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
    if(!filePath.isEmpty())
    {
        //裁剪路径
        QStringList list = filePath.split("/");
        //设置标题
        this->setWindowTitle(list[list.length() - 1]);
        QFile * file = new QFile;
        file->setFileName(filePath);
        bool isOpen = file->open(QIODevice::ReadOnly);
        if(isOpen)
        {
            QTextStream in(file);
            QString text = in.readAll();
            this->ui->textEdit->setText(text);
            file->close();
            delete file;
        }
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

这段代码类似记事本的【打开】功能

  • 2.字体
//字体
void MainWindow::on_menu_font_triggered()
{
    //获取选中的字体
    bool ok;
    QFont font = QFontDialog::getFont(&ok,this);
    if(ok)
    {
        this->ui->textEdit->setFont(font);
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 3.颜色
//设置字体颜色
void MainWindow::on_menu_color_triggered()
{
    QColor color = QColorDialog::getColor(Qt::black, this);
    if(color.isValid())
    {
        this->ui->textEdit->setTextColor(color);
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 4.时间
//时间
void MainWindow::on_menu_time_triggered()
{
    QDateTime dataTime = QDateTime::currentDateTime();
    QString time= dataTime.toString("yyyy-MM-dd HH:mm:ss");
    this->ui->textEdit->append(time);
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

5.打开网站

    //打开网页
    QDesktopServices::openUrl(QUrl("https://www.baidu.com/"));
  
  
  • 1
  • 2

6.Gif

    //播放Gif
    QMovie * movie = new QMovie("/img/xx.gif");
    this->ui->label->setMovie(movie);
    movie->start();
    //movie->stop();
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

7.启动页

  int main(int argc, char *argv[])
  {
      QApplication app(argc, argv);
      QPixmap pixmap(":/splash.png");
      QSplashScreen splash(pixmap);
      splash.show();
      app.processEvents();
      ...
      QMainWindow window;
      window.show();
      splash.finish(&window);
      return app.exec();
  }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

8.播放声音

QSound bells("mysounds/bells.wav");
bells.play();
//对应的pause stop
  
  
  • 1
  • 2
  • 3

9.读取文件属性

void MainWindow::openFile()
{
    QString fileName = QFileDialog::getOpenFileName(this,"选择文件",QDir::homePath());
    if(!fileName.isEmpty())
    {
        this->ui->textBrowser->clear();
        qDebug() << fileName;
        //标题
        this->setWindowTitle(fileName.split("/")[fileName.split("/").size() -1]);

        QFileInfo info(fileName);
        //文件大小
        int size = info.size() / 1024 / 1024;
        this->ui->textBrowser->append("文件大小:" + QString::number(size) + "MB");
        //创建时间
        QString time = info.created().toString("yyyy-MM-dd HH:mm:ss");
        this->ui->textBrowser->append("创建时间:" + time);
        //最后访问时间
        QString lastTime = info.lastRead().toString("yyyy-MM-dd HH:mm:ss");
    }
    else
    {
        qDebug() << "ERROR:获取文件失败";
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

10.List Widget

//插入
void MainWindow::on_btn_add_clicked()
{
    QListWidgetItem* item = new QListWidgetItem;
    item->setText("你好");
    this->ui->listWidget->addItem(item);
}
//删除
void MainWindow::on_btn_del_clicked()
{
    //条目总数
    //this->ui->listWidget->count();
    //选中条目
    int row = this->ui->listWidget->currentRow();
    this->ui->listWidget->takeItem(row);
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

11.自定义应用图标

你可以在帮助中搜索-Setting the Application icon就可以找到办法了

  • 1.图片为ico后缀
  • 2.拷贝到根目录
  • 3.新建一文件 xx.rc
  • 4.内容为
IDI_ICON1 ICON DISCARDABLE "xx.icon"
  
  
  • 1
  • 5.pro中修改
RC_FILE += xx.rc
  
  
  • 1

12.文本编码

#include "mainwindow.h"
#include <QApplication>

#include <QTextCodec>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //设置编码
    QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());
    QLabel*label = new QLabel;
    label->setText(QObject::tr("Hello World!"));
    label->show();
    return a.exec();
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

13.提示框

void MainWindow::on_listWidget_itemClicked(QListWidgetItem *item)
{
    if(item->text() == tr("颜色对话框")){
        QColor color = QColorDialog::getColor(Qt::red,this,tr("选择颜色"));
        item->setTextColor(color);
    }else if(item->text() == tr("字体对话框")){
        bool ok;
        QFont font = QFontDialog::getFont(&ok,this);
        if(ok){
            item->setFont(font);
        }
    }else if(item->text() == tr("问题对话框")){
        QMessageBox::question(this,tr("对话框"),tr("这是问题对话框"),QMessageBox::Ok,QMessageBox::No);
    }else if(item->text() == tr("提示对话框")){
        QMessageBox::information(this,tr("对话框"),tr("这是提示对话框"),QMessageBox::Ok);
    }else if(item->text() == tr("警告对话框")){
        QMessageBox::warning(this,tr("对话框"),tr("这是警告对话框"),QMessageBox::Abort);
    }else if(item->text() == tr("错误对话框")){
        QMessageBox::critical(this,tr("对话框"),tr("这是错误对话框"),QMessageBox::YesAll);
    }else if(item->text() == tr("关于对话框")){
        QMessageBox::about(this,tr("对话框"),tr("这是关于对话框"));
    }else if(item->text() == tr("文件对话框")){
        QString path = QFileDialog::getOpenFileName(this,tr("选择文件"),QDir::currentPath(),tr("图片文件(*png*jpg)"));
        item->setText(path);
    }else if(item->text() == tr("输入对话框")){
        bool ok;
        QInputDialog::getText(this,tr("输入内容"),tr("请输入用户名"),QLineEdit::Normal,tr("admin"),&ok);
    }else if(item->text() == tr("进度对话框")){
        QProgressDialog dialog(tr("文件复制进度"),tr("取消"),0,50000,this);
        dialog.show();
        for (int i = 0; i < 500000000000; ++i) {
            dialog.setValue(i);
            //避免界面冻结
            QCoreApplication::processEvents();
            if(dialog.wasCanceled()){
                break;
            }
        }
        dialog.setValue(500000000000);
    }else if(item->text() == tr("向导对话框")){

        QWizardPage* page1 = new QWizardPage;
        page1->setTitle("页面1");
        QWizardPage* page2 = new QWizardPage;
        page2->setTitle("页面2");
        QWizardPage* page3 = new QWizardPage;
        page3->setTitle("页面3");

        QWizard wizard;
        wizard.addPage(page1);
        wizard.addPage(page2);
        wizard.addPage(page3);

        wizard.exec();

    }else if(item->text() == tr("错误信息对话框")){
        QErrorMessage*message = new QErrorMessage;
        message->setWindowTitle("错误信息对话框");
        message->showMessage("这是错误");
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

14.输入限制

    //输入限制0-100以内
    QValidator* validator = new QIntValidator(0,100,this);
    this->ui->lineEdit->setValidator(validator);
  
  
  • 1
  • 2
  • 3

剩下的我会不定期的更新

一起交流可以加群:690351511

猜你喜欢

转载自blog.csdn.net/weixin_39485901/article/details/88864555