QT开发(十)—— 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;

5.九九乘法表

    //九九乘法表
    for (int i = 1; i <= 9; i++)
    {
        for (int j = 1;j<=i;j++)
        {
            cout << i << " * " << j << " = " << i * j << " ";
        }
        cout << endl;
    }

6.预处理命令

#define
#error
#if
#else
#elif
#endif  
#ifdef
#ifndef
#undef
#line
#pragma

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;
}

二.QT

1.LineEdit

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

这里写图片描述

2.控件移动

//动态移动
setGeometry(30,30,3,0)

3.启动程序

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

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;
        }
    }
}

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

  • 2.字体
//字体
void MainWindow::on_menu_font_triggered()
{
    //获取选中的字体
    bool ok;
    QFont font = QFontDialog::getFont(&ok,this);
    if(ok)
    {
        this->ui->textEdit->setFont(font);
    }
}
  • 3.颜色
//设置字体颜色
void MainWindow::on_menu_color_triggered()
{
    QColor color = QColorDialog::getColor(Qt::black, this);
    if(color.isValid())
    {
        this->ui->textEdit->setTextColor(color);
    }
}
  • 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);
}

5.打开网站

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

剩下的我会不定期的更新

一起交流可以加群:690351511

猜你喜欢

转载自blog.csdn.net/qq_26787115/article/details/80244219
今日推荐