qt调用cmd.exe弹出窗口5.15.2
包含
#include <Windows.h>
#include "mainwindow5_7a.h" #include "ui_mainwindow5_7a.h" #include <QDragEnterEvent> #include <QUrl> #include <QFile> #include <QTextStream> #include <QMimeData> #include <QDebug> #include <QMessageBox> #include <QProcess> #include <Windows.h> //QT拖放读取图片文件打开图像 MainWindow5_7a::MainWindow5_7a(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow5_7a) { ui->setupUi(this); //QProcess m_process; QString program = "C:\\WINDOWS\\system32\\cmd.exe"; QStringList list; list << "/c dir"; list <<""; // list << QString("rundll32.exe") + QString(" C:\\Windows\\System32\\shimgvw.dll,ImageView_Fullscreen ") + path; //if (m_process.state() != QProcess::NotRunning) { // m_process.close(); } //m_process.start("D:/QT_Project_vs/SugarTvision/x64/Debug/SugarTvision.exe", list); // m_process.startDetached(program, list); QStringList arguments; arguments << "/K"; arguments << "title"; arguments << "child process"; QProcess *myProcess = new QProcess(this); //QString program = "D:/Work/Code/VsProj/BasicSytax/Qt/QtProcess/x64/Debug/ConsoleTest.exe"; //QString program = "C:/Windows/System32/notepad.exe"; program = "C:/Windows/System32/cmd.exe"; myProcess->setCreateProcessArgumentsModifier([](QProcess::CreateProcessArguments *args) { //args->startupInfo->wShowWindow = SW_SHOWMAXIMIZED;//此成员变量设置此有效,QT5.12 args->flags |= CREATE_NEW_CONSOLE; args->startupInfo->dwFlags &= ~STARTF_USESTDHANDLES; args->startupInfo->dwFlags |= STARTF_USEFILLATTRIBUTE; args->startupInfo->dwFillAttribute = BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY; //args->startupInfo->dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; } ); myProcess->start(program, arguments); setAcceptDrops(true); } MainWindow5_7a::~MainWindow5_7a() { delete ui; } void MainWindow5_7a::dragEnterEvent(QDragEnterEvent *event) { if(event->mimeData()->hasUrls())//数据中是否包含URL { event->acceptProposedAction();//接受动作 } else { event->ignore();//忽略该事件 qDebug()<<"无效文件哦"; } } void MainWindow5_7a::dropEvent(QDropEvent *event) { const QMimeData * q_mimedata=event->mimeData();//获取MIME的数据 if(q_mimedata->hasUrls()) { QList<QUrl> url_list= q_mimedata->urls(); //获取URL列表 foreach(QUrl aa,url_list) { qDebug()<<aa.toLocalFile()<<","; } //将其中的一个URL表示为本地文件的路径 QString file_name=url_list.at(0).toLocalFile(); // if(!file_name.isEmpty()) // { // QFile file2(file_name); // if(!file2.open(QIODevice::ReadOnly)) // { // return; // } // QTextStream in(&file2); //建立文本流对象 // //ui->textEdit->clear(); // ui->textEdit->setText(in.readAll());//读取流对象的内容 // } // else // { // qDebug()<<"文件空的哦"; // } QPixmap pixmap(file_name); ui->label_pic->setPixmap(pixmap); ui->label_pic->resize(pixmap.height(),pixmap.width()); } else { qDebug()<<"URL不存在哦"; } } void MainWindow5_7a::on_pushButton_clicked() { QMessageBox::information(this,tr("tips"),tr("图像处理事件")); }
欢迎讨论,相互学习。
cdtxw@foxmail.com