在QT中添加VTK
在main函数中初始化
//VTK的初始化语句
#ifndef INITIAL_OPENGL
#define INITIAL_OPENGL
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
VTK_MODULE_INIT(vtkRenderingFreeType);
#endif
在QT中全屏显示界面以及界面的选择
所需的头文件
#include <QDesktopWidget>
main()函数代码
QApplication a(argc, argv);
UI_Designer w;
QDesktopWidget *desktop = QApplication::desktop();
int screen_count = desktop->screenCount();
w.setGeometry(desktop->screenGeometry(1));//多显示器选择
w.showFullScreen();
//w.show();
给QT的窗口添加背景图像
c
//添加背景
QPixmap pixmap("Resources/icon/1.jpg");
QPalette palette;
palette.setBrush(backgroundRole(), QBrush(pixmap));
setPalette(palette);
setAutoFillBackground(true);
//show();
QT中中文显示乱码
c
#include <qtextcodec.h>
QTextCodec *codec = QTextCodec::codecForName("GBK");//修改这两行
w.setWindowTitle(codec->toUnicode("学生事务管理系统"));
QT Designer中贴图设置
在控件中的styleSheet中设置图片资源
#frame {
border-image: url(:/UI_Designer/Resources/icon/test_Frame.png);
}
#frame * {
border-image: url(:)
}
其中#frame{}表示frame这个控件
#frame * { }表示frame控件中的子控件
双滑块的滑动条调用
添加重新继承的Slider代码,包含Qxtspanslider.h、Qxtspanslider_p.h、QxtSpanSlider.cpp这三个文件。
在QT Designer中选中QSlider右键提升为,然后输入
提升类的名称:QxtSpanSlider
头文件:Qxtspanslider.h
最后点击添加即可。
ui.verticalSlider->setHandleMovementMode(QxtSpanSlider::NoOverlapping);//设置滑块不能越界
ui.verticalSlider->setMaximum(307100);//设置最大值
ui.verticalSlider->setMinimum(-204800);//设置最小值
ui.verticalSlider->setLowerValue(0);//设置当前最小值
ui.verticalSlider->setUpperValue(25000);//设置当前最大值
ui.verticalSlider->setTickInterval(10000);//设置标尺的间隔
ui.verticalSlider->setTickPosition(QSlider::TicksAbove);//设置标尺
//槽函数
connect(ui.verticalSlider, SIGNAL(lowerValueChanged(int)), this, SLOT(lowerValueChangedSlot(int)));
connect(ui.verticalSlider, SIGNAL(upperValueChanged(int)), this,SLOT(upperValueChangedSlot(int)));
QT全屏显示
#include <QtPlatformHeaders/QWindowsWindowFunctions>
PET_CT_show w;
QDesktopWidget *desktop = QApplication::desktop();
int screen_count = desktop->screenCount();
w.setGeometry(desktop->screenGeometry(1));
//下面一条语句一定要加,不然全屏显示的时候会有bug
QWindowsWindowFunctions::setHasBorderInFullScreen(w.windowHandle(), true);
w.showFullScreen();
vtk中进行颜色映射
//彩色映射
vtkSmartPointer<vtkNIFTIImageReader> reader_NII = vtkSmartPointer<vtkNIFTIImageReader>::New();
reader_NII->SetFileName(zj_nii_path.toStdString().data());
reader_NII->Update();
vtkSmartPointer<vtkLookupTable> colorTable =vtkSmartPointer<vtkLookupTable>::New();
colorTable->SetRange(0.0, 14815);
colorTable->SetHueRange(-0.5, 1);//0.1 0.5
colorTable->SetValueRange(0, 1);//0.6 1.0
colorTable->Build();
vtkSmartPointer<vtkImageMapToColors> colorMap =
vtkSmartPointer<vtkImageMapToColors>::New();
colorMap->SetInputConnection(reader_NII->GetOutputPort());
colorMap->SetLookupTable(colorTable);
colorMap->Update();
vtkSmartPointer<vtkImageActor> shiftscaleActor =
vtkSmartPointer<vtkImageActor>::New();
shiftscaleActor->SetInputData(colorMap->GetOutput());
page_6_imageViewer_YZ->SetInputData(colorMap->GetOutput());
ui.qvtkWidget_page_7_2->SetRenderWindow(page_6_imageViewer_YZ->GetRenderWindow());
page_6_imageViewer_YZ->SetSliceOrientationToXY();
vtkSmartPointer<myVtkInteractorStyleImage> page_6_myInteractorStyle_YZ = vtkSmartPointer<myVtkInteractorStyleImage>::New();
page_6_myInteractorStyle_YZ->SetImageViewer(page_6_imageViewer_YZ, ui.horizontalSlider_YZ);
page_6_imageViewer_YZ->SetupInteractor(page_6_renWin_YZ);
page_6_imageViewer_YZ->SetSlice((page_6_imageViewer_YZ->GetSliceMax() - page_6_imageViewer_YZ->GetSliceMin()) / 2);
page_6_renWin_YZ->SetInteractorStyle(page_6_myInteractorStyle_YZ);
//设置滑块
ui.horizontalSlider_YZ->setMinimum(page_6_imageViewer_YZ->GetSliceMin());
ui.horizontalSlider_YZ->setMaximum(page_6_imageViewer_YZ->GetSliceMax());
ui.horizontalSlider_YZ->setValue((page_6_imageViewer_YZ->GetSliceMax() - page_6_imageViewer_YZ->GetSliceMin()) / 2);
//设置数据框
ui.spinBox_YZ->setRange(page_6_imageViewer_YZ->GetSliceMin(), page_6_imageViewer_YZ->GetSliceMax());
ui.spinBox_YZ->setValue((page_6_imageViewer_YZ->GetSliceMax() - page_6_imageViewer_YZ->GetSliceMin()) / 2);
//XY
page_6_imageViewer_YZ->GetRenderer()->ResetCamera();
vtkCamera *page_6_aCamera_YZ = vtkCamera::New();
page_6_aCamera_YZ = page_6_imageViewer_YZ->GetRenderer()->GetActiveCamera();
page_6_aCamera_YZ->Zoom(1.45);//图像缩放
page_6_imageViewer_YZ->GetRenderer()->SetActiveCamera(page_6_aCamera_YZ);
//page_2_imageViewer_XY->GetRenderer()->SetBackground(1, 0, 0);
page_6_imageViewer_YZ->Render();
//-----------------------------------------------------------------------
page_6_renWin_YZ->Start();
将jpg读入内存后用vtk显示
//获取per_path的地址
QDir dir_per(per_path);
if (dir_per.exists())
{
//获取per文件路径下的文件夹和路径
dir_per.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);//文件夹|文件|不包含./和../
//排序文件夹优先
//dir_per.setSorting(QDir::Files);
//获取文件后缀是jpg的文件,如果使用则只搜索当前文件夹下的文件
QStringList filer_per;
filer_per << "*.jpg";
list_per.clear();
list_per = dir_per.entryInfoList(filer_per, QDir::Files, 0x00);
//读取所有的per_jpg图片到内存中
for (int i = 0; i < list_per.size(); i++)
{
per_jpg_file_path.push_back(per_path + QString("%1").arg(i) + ".jpg");
per_jgp_image.push_back(vtkSmartPointer<vtkJPEGReader>::New());
per_jgp_image[i]->SetFileName(per_jpg_file_path[i].toLatin1().data());
}
}
//vtk显示
//显示per
page_1_imageViewer_per->SetInputConnection(per_jgp_image[int(list_per.size() / 2)]->GetOutputPort());//SetInputConnection
ui.qvtkWidget_page_2_1->SetRenderWindow(page_1_imageViewer_per->GetRenderWindow());
//page_1_imageViewer_per->SetSliceOrientationToXY();
page_1_myInteractorStyle_per->SetImageViewer(page_1_imageViewer_per, ui.horizontalSlider_XY, per_jgp_image, Page_Index, list_per);
page_1_imageViewer_per->SetupInteractor(page_1_renWin_per);
page_1_renWin_per->SetInteractorStyle(page_1_myInteractorStyle_per);
//设置滑块
ui.horizontalSlider_XY->setMinimum(0);
ui.horizontalSlider_XY->setMaximum(list_per.size() - 1);
ui.horizontalSlider_XY->setValue(int(list_per.size() / 2));
//设置数据框
ui.spinBox_XY->setRange(0, list_per.size() - 1);
ui.spinBox_XY->setValue(int(list_per.size() / 2));
page_1_imageViewer_per->GetRenderer()->ResetCamera();
vtkCamera *page_1_aCamera_per = vtkCamera::New();
page_1_aCamera_per = page_1_imageViewer_per->GetRenderer()->GetActiveCamera();
page_1_aCamera_per->Zoom(1.45);
page_1_imageViewer_per->GetRenderer()->SetActiveCamera(page_1_aCamera_per);
page_1_imageViewer_per->Render();//显示图像
open_algorithm_page2 = 1;
page_1_renWin_per->Start();
删除文件夹及文件夹下的所有文件
void RemoveFiles(string dir)
{
//在目录后面加上"\\*.*"进行第一次搜索
string newDir = dir + "\\*.*";
//用于查找的句柄
intptr_t handle;
struct _finddata_t fileinfo;
//第一次查找
handle = _findfirst(newDir.c_str(), &fileinfo);
if (handle == -1) {
cout << "无文件" << endl;
system("pause");
return;
}
do
{
if (fileinfo.attrib & _A_SUBDIR) {//如果为文件夹,加上文件夹路径,再次遍历
if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)
continue;
// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
newDir = dir + "\\" + fileinfo.name;
listFiles(newDir.c_str());//先遍历删除文件夹下的文件,再删除空的文件夹
cout << newDir.c_str() << endl;
if (_rmdir(newDir.c_str()) == 0) {//删除空文件夹
cout << "delete empty dir success" << endl;
}
else {
cout << "delete empty dir error" << endl;
}
}
else {
string file_path = dir + "\\" + fileinfo.name;
cout << file_path.c_str() << endl;
if (remove(file_path.c_str()) == 0) {//删除文件
cout << "delete file success" << endl;
}
else {
cout << "delete file error" << endl;
}
}
} while (!_findnext(handle, &fileinfo));
_findclose(handle);
return;
}
调用:
//生成了文件,顺便删除中间过程
QString delete_path = get_exe_result_path + "\\processed_patches";
std::string dirName = delete_path.toStdString();
//RemoveDirectory(dirName.c_str());
QDir dir(delete_path);
if (dir.exists())
{
RemoveFiles(dirName);
rmdir(dirName.c_str());
}