MainWindow
::MainWindow(QWidget *parent
){
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::on_addExeButton_clicked()
{
QString filename
= QFileDialog::getOpenFileName(this, tr
("choose the file or application"), qApp
->applicationDirPath
(), tr
("Excutable(*.*)"));
if(filename.isNull())
return false;
//User chooses a file that has been chosen before
if(buttons.keys().contains(filename))
{
QMessageBox::critical(this, tr
("Existed File"), tr
("Sorry but the file you choose has been chose before.\n" "check the buttons on the interface!!"));
return false;
}
//User chooses a new file which has not been selected before
QIcon icon
= seekIcon.
icon(fileInfo
);
btn->resize( 500, 500);
btn->setIcon(icon);
connect(btn, SIGNAL(clicked()), this, SLOT(callExe()));
//click the button to run the corresponding exe or open the file
buttons.insert(filename, btn);
ui->verticalLayout1->addWidget(btn);
ui->verticalLayout1->addStretch();
qDebug() << buttons.size();
return true;
}
void MainWindow::callExe()
{
qDebug() << "Ready to execute program";
QToolButton *sender
= qobject_cast<QToolButton
*>
(this
->sender
());
//This SLOT is called by these inner code, not the user, then return
if(!sender)
{
return;
}
qDebug() << "Sender found successfully!";
while(i.hasNext())
{
// qDebug() << "Enter the while loop successfully!";
if(i.peekNext().value() == sender)
{
path = i.next().key();
qDebug() << "Find the path!!\n";
break;
}
else
{
i.next();
}
}
if(path.isEmpty())
{
qDebug() << "******File path is null!!";
return;
}
//打开相应程序 In English------Using QProcess to run the applications
//Using class QDesktopServices to run the files or applications
{
qDebug() << "Fail to run the file";
}
qDebug() << "SLOT successfully!" << endl;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindowClass)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::on_addExeButton_clicked()
{
QString filename = QFileDialog::getOpenFileName(this, tr("choose the file or application"), qApp->applicationDirPath(), tr("Excutable(*.*)"));
if(filename.isNull())
return false;
//User chooses a file that has been chosen before
if(buttons.keys().contains(filename))
{
QMessageBox::critical(this, tr("Existed File"), tr("Sorry but the file you choose has been chose before.\n"
"check the buttons on the interface!!"));
return false;
}
//User chooses a new file which has not been selected before
QFileInfo fileInfo(filename);
QFileIconProvider seekIcon;
QIcon icon = seekIcon.icon(fileInfo);
QToolButton *btn = new QToolButton;
btn->resize( 500, 500);
btn->setIcon(icon);
connect(btn, SIGNAL(clicked()), this, SLOT(callExe()));
//click the button to run the corresponding exe or open the file
buttons.insert(filename, btn);
ui->verticalLayout1->addWidget(btn);
ui->verticalLayout1->addStretch();
qDebug() << buttons.size();
return true;
}
void MainWindow::callExe()
{
qDebug() << "Ready to execute program";
QToolButton *sender = qobject_cast<QToolButton*>(this->sender());
//This SLOT is called by these inner code, not the user, then return
if(!sender)
{
return;
}
qDebug() << "Sender found successfully!";
QString path;
QMapIterator<QString, QToolButton *> i(buttons);
while(i.hasNext())
{
// qDebug() << "Enter the while loop successfully!";
if(i.peekNext().value() == sender)
{
path = i.next().key();
qDebug() << "Find the path!!\n";
break;
}
else
{
i.next();
}
}
if(path.isEmpty())
{
qDebug() << "******File path is null!!";
return;
}
//打开相应程序 In English------Using QProcess to run the applications
//Using class QDesktopServices to run the files or applications
if(!QDesktopServices::openUrl(QUrl(path)))
{
qDebug() << "Fail to run the file";
}
qDebug() << "SLOT successfully!" << endl;
}
To copy to clipboard, switch view to plain text mode
Bookmarks