From time to time, I will summarize some of the tips I have discovered, so that even if I forget it, I can find it back by flipping through the blog, which is also excellent.
1. C++
1. Modification of variables
- auto: belongs to one-time storage, and its storage space can be repeatedly overwritten by several variables
- register: stored in a general-purpose register
- extern: can be referenced in all functions and program sections
- static: stored at a fixed address in memory, valid during the entire program running
2. Display the conversion operator
- static_case
- dynameic_cast
- const_cast
- reinterpret_cast
3. The default value of bool
- true=1
- false=0
4. Binary output
//进制输出
int a = 1000;
cout << "十进制:"<< dec << a << endl;
cout << "八进制:" << oct << a << endl;
cout << "十六进制:" << hex << a << endl;
5. Ninety-nine multiplication table
//九九乘法表
for (int i = 1; i <= 9; i++)
{
for (int j = 1;j<=i;j++)
{
cout << i << " * " << j << " = " << i * j << " ";
}
cout << endl;
}
6. Preprocessing commands
#define
#error
#if
#else
#elif
#endif
#ifdef
#ifndef
#undef
#line
#pragma
7. Array length
C++ does not directly provide the length method of the array, we can calculate it like this
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;
}
2. QT
1.LineEdit
- 1. Enter the password
this->ui->lineEdit->setEchoMode(QLineEdit::Password);
- 2. Smart Tips
//可以设置匹配模式
QStringList list;
list << "1" << "12" << "123"<< "1234"<< "12345";
QCompleter * completer = new QCompleter(list,this);
this->ui->lineEdit->setCompleter(completer);
2. Control movement
//动态移动
setGeometry(30,30,3,0)
3. Start the program
QProcess *myProcess = new QProcess();
//获取到输入框输入的内容启动,类似cmd
myProcess->start(this->ui->cmd_line->text().trimmed());
4.Notepad
Tips accumulated recently in notepad
- 1. Open the file
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;
}
}
}
This code is similar to the [Open] function of Notepad
- 2. Fonts
//字体
void MainWindow::on_menu_font_triggered()
{
//获取选中的字体
bool ok;
QFont font = QFontDialog::getFont(&ok,this);
if(ok)
{
this->ui->textEdit->setFont(font);
}
}
- 3. Color
//设置字体颜色
void MainWindow::on_menu_color_triggered()
{
QColor color = QColorDialog::getColor(Qt::black, this);
if(color.isValid())
{
this->ui->textEdit->setTextColor(color);
}
}
- 4. Time
//时间
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. Open the website
//打开网页
QDesktopServices::openUrl(QUrl("https://www.baidu.com/"));
I will update the rest from time to time