我正在将Qt Creator用于GUI应用程序。我正在使用Ubuntu OS。
我在GUI中有两个按钮,但是除非按下另一个按钮,否则它将无法工作。
如果我按下一个按钮,它将不会显示任何输出;如果我关闭主窗口,则会在控制台上打印输出。
这是头文件
#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H
#include <QPushButton>
#include <QFileDialog>
#include <QMessageBox>
#include <QMainWindow>
#include <QVBoxLayout>
class MyMainWindow: public QMainWindow
{
Q_OBJECT
public:
MyMainWindow(QWidget * parent = 0);
virtual ~ MyMainWindow();
private slots:
void clickedRun();
void filechooser();
private:
QPushButton *button,*button2;
std::string getFileExt(std::string s);
};
#endif // MYMAINWINDOW_H
这是cpp文件
#include <QPushButton>
#include <QFileDialog>
#include <QMessageBox>
#include <QMainWindow>
#include <QVBoxLayout>
#include <iostream>
#include <QDebug>
QStringList Listoffiles;
std::string headerfilename,sourcefile;
MyMainWindow::MyMainWindow(QWidget * parent) : QMainWindow (parent)
{
setWindowTitle("welcome");
QPushButton *button = new QPushButton(this);
QPushButton *button2 = new QPushButton(this);
button->setText("Choose Files");
button2->setText("Run");
QObject::connect(button, SIGNAL(clicked()),this, SLOT(filechooser()));
QObject::connect(button2, SIGNAL(clicked()),this, SLOT(clickedRun()));
button->move(100, 100);
button->show();
button2->move(300,100);
button2->show();
setWindowTitle("Pushbutton Clicked Signal Test");
show();
}
MyMainWindow::~MyMainWindow()
{
}
void MyMainWindow::clickedRun()
{
qDebug() << "inside run \n";
}
void MyMainWindow::filechooser()
{
QFileDialog dialog(this);
dialog.setFileMode(QFileDialog::ExistingFiles);
//dialog.setFileMode(QFileDialog::AnyFile);
if(dialog.exec())
{
Listoffiles = dialog.selectedFiles();
}
std::cout << Listoffiles.count() <<" Files selected \n";
int i = 0;
for(QString q: Listoffiles)
{
qDebug()<<"how many times "<< ++i;
std::string file = q.toUtf8().constData();
if(getFileExt(file)=="h")
{
headerfilename = file;
std::cout << "\nIncluded header file : ";
std::cout << headerfilename;
}else if(getFileExt(file)=="cpp")
{
sourcefile = file;
std::cout << "\nFile selected to run : " << sourcefile << std::endl;
}
}
dialog.close();
}
std::string MyMainWindow::getFileExt(std::string s){
size_t i = s.rfind('.',s.length());
if(i!= std::string::npos)
{
return (s.substr(i+1,s.length()-1));
}
return ("");
}
这是主文件
#include <QApplication>
#include <QMainWindow>
#include "MyMainWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyMainWindow window;
//window.setWindowTitle(QString::fromUtf8("MainWindow"));
window.resize(700,500);
window.show();
return app.exec();
}
我该如何解决这个问题。