//进制输出
int a = 1000;
cout << "十进制:"<< dec << a << endl;
cout << "八进制:" << oct << a << endl;
cout << "十六进制:" << hex << a << endl;
//九九乘法表
for (int i = 1; i <= 9; i++)
{
for (int j = 1;j<=i;j++)
{
cout << i << " * " << j << " = " << i * j << " ";
}
cout << endl;
}
#define
#error
#if
#else
#elif
#endif
#ifdef
#ifndef
#undef
#line
#pragma
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;
}
this->ui->lineEdit->setEchoMode(QLineEdit::Password);
//可以设置匹配模式
QStringList list;
list << "1" << "12" << "123"<< "1234"<< "12345";
QCompleter * completer = new QCompleter(list,this);
this->ui->lineEdit->setCompleter(completer);
//动态移动
setGeometry(30,30,3,0)
QProcess *myProcess = new QProcess();
//获取到输入框输入的内容启动,类似cmd
myProcess->start(this->ui->cmd_line->text().trimmed());
最近做记事本积累下来的小技巧
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;
}
}
}
这段代码类似记事本的【打开】功能
//字体
void MainWindow::on_menu_font_triggered()
{
//获取选中的字体
bool ok;
QFont font = QFontDialog::getFont(&ok,this);
if(ok)
{
this->ui->textEdit->setFont(font);
}
}
//设置字体颜色
void MainWindow::on_menu_color_triggered()
{
QColor color = QColorDialog::getColor(Qt::black, this);
if(color.isValid())
{
this->ui->textEdit->setTextColor(color);
}
}
//时间
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);
}
//打开网页
QDesktopServices::openUrl(QUrl("https://www.baidu.com/"));
//播放Gif
QMovie * movie = new QMovie("/img/xx.gif");
this->ui->label->setMovie(movie);
movie->start();
//movie->stop();
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();
}
QSound bells("mysounds/bells.wav");
bells.play();
//对应的pause stop
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:获取文件失败";
}
}
//插入
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);
}
你可以在帮助中搜索-Setting the Application icon就可以找到办法了
IDI_ICON1 ICON DISCARDABLE "xx.icon"
RC_FILE += xx.rc
#include "mainwindow.h"
#include
#include
#include
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();
}
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("这是错误");
}
}
//输入限制0-100以内
QValidator* validator = new QIntValidator(0,100,this);
this->ui->lineEdit->setValidator(validator);
剩下的我会不定期的更新