文章目录
- 信号和槽
- 使用弹簧做界面
- 方便Debug语句
- 设置标题添加状态栏
- 显示图片
- 文件操作
- QByteArray转int
- QJson类读GeoJson文件
- TreeView的简单使用
- Qt OpenGL的使用
- gdal的编译及Qt加载
信号和槽
- connect
- 槽函数
- 设计界面 不能自定义
connect 更喜欢用这种形式:匿名函数+函数
connect(fontWidget,&QDialog::accepted,[this](){
});
connect(fontWidget,&QDialog::accepted,this,&MainWindow::changeFonts);
void MainWindow::changeFonts()
{
mout<<"改变字";
mout<<fontWidget->getFontSize();
ui->textEdit->setFontItalic(fontWidget->isitalic());
ui->textEdit->setFontPointSize(fontWidget->getFontSize());
ui->textEdit->setFont(fontWidget->getFont());
}
使用弹簧做界面
方便Debug语句
#include <QtDebug>
#define mout qDebug() <<"["<<__FILE__<<":"<<__LINE__<<"]"
mout<<"";
设置标题添加状态栏
//设标题
this->setWindowTitle("EncodeDa");
//状态栏
this->myStatusBar = statusBar();
this->myStatusBarLabel = new QLabel(this);
myStatusBarLabel->setText("字符:0,单词数:0");
myStatusBar->addWidget(myStatusBarLabel);
显示图片
QImage* img = new QImage;
QLabel* scrImg1 = new QLabel(this);
scrImg1->setGeometry(150,10,380,240);
img->load("F:/Reverse.bmp");
scrImg1->setPixmap(QPixmap::fromImage(img->scaled(230,230,Qt::KeepAspectRatio)));
emit scrImg3->clear();//可清除内容
文件操作
- 打开文件
- 按长度读取
- 获取文件信息
- 写入文件
- 文件打开保存对话框
打开文件
按长度读取
#include<QFile>
QFile file(path);
bool isOK = file.open(QIODevice::ReadOnly);//打开文件
if(isOK == false){
mout <<"文件打开失败";
}else{
//读取文件
QByteArray s = file.read(sizeof (char));//read有几种用法参考提示。
QByteArray w = file.read(sizeof (int));
}
file.close();//勿忘
//也可以使用
#include<QDataStream>
获取文件信息
#include<QFileInfo>
//显示文件信息
QFileInfo info(path);
QString fileInfo = QString("文件名:%1 文件大小:%2 文件创建日期:%3")
.arg(info.fileName().toUtf8().data()).arg(info.size())
.arg(info.birthTime().toString("yyyy-MM-dd hh:mm:ss"));
ui->label_FileInfo->setText(fileInfo);
写入文件
QFile file;
file.setFileName(fileName);
bool isOK = file.open(QIODevice::WriteOnly);
if(isOK == true)
{
QString str = ui->textEdit->toPlainText();
//写文件需转UTF-8
file.write(str.toLocal8Bit());
}else{
mout <<"保存失败";
}
file.close();//勿忘
文件打开保存对话框
#include<QFileDialog>
QString path = QFileDialog::getOpenFileName(this,"标题","此处初始目录”,"JPEG Files(*.jpg);;PNG Files(*.png)");
if(path.isEmpty() == true)
{
mout <<"目录为空";
}else{
}
QString fileName = QFileDialog::getSaveFileName(this,"标题","../1.txt","TXT(*.txt)");
if(fileName.isEmpty() == true)
{
mout <<"路径为空";
}else{
}
QByteArray转int
int MainWindow::bytesToInt(QByteArray bytes) {
int addr = bytes[0] & 0x000000FF;
addr |= ((bytes[1] << 8) & 0x0000FF00);
addr |= ((bytes[2] << 16) & 0x00FF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
return addr;
}
QJson类读GeoJson文件
GeoJson文件格式规范说明
GeoJson数据下载
也可以从ArcGIS导出GeoJson数据
QJson类前人经验
主要是object、value、array之间的关系,注意对变体型的应用,给出一些读取结构截图
点
线
面
TreeView的简单使用
直接参考这个文章
获取TreeView数据的代码
void MainWindow::slotMapTreeMenutoQString(bool checked)
{
QModelIndex curIndex = ui->mapTreeView->currentIndex();
QModelIndex index = curIndex.sibling(curIndex.row(),2); //同一行第3列元素的index
QModelIndex indexP1 = index.parent().sibling(index.parent().row(),2);
QModelIndex indexP2 = indexP1.parent().sibling(indexP1.parent().row(),2);
if(index.isValid())
{
if(indexP1.isValid()){
if(indexP2.isValid()){
//三级节点
this->showMessage(this->maps.at(indexP2.data().toInt())
->getLayer(indexP1.data().toInt())
->getObj(index.data().toInt())->toQString());
ui->textBrowser->setText(this->maps.at(indexP2.data().toInt())
->getLayer(indexP1.data().toInt())
->getObj(index.data().toInt())->toQString());
}else{
//二级节点
this->showMessage(this->maps.at(indexP1.data().toInt())->getLayer(index.data().toInt())->toQString());
ui->textBrowser->setText(this->maps.at(indexP1.data().toInt())->getLayer(index.data().toInt())->toQString());
}
}else{
//一节节点
this->showMessage(this->maps.at(index.data().toInt())->toQString());
ui->textBrowser->setText(this->maps.at(index.data().toInt())->toQString());
}
}
}
Qt OpenGL的使用
Learn OpenGL
主要参考这个
和这个
//调用Widget::Updata()可以重绘
emit openGLWidget->update();
gdal的编译及Qt加载
这篇文章