Qt中的类静态函数调用
近日在学习Qt时,发现了Qt类中有静态成员函数,不同于Public Functions
,Static Public Members
可以通过创建类对象,new一个指针对象,或者直接调用类中的函数进行,下面以直接调用类中的函数举例:
#include <QFileDialog>
void openFile(){
/* 不创建对象,直接通过类的静态函数接口进行调用 */
QString filePath = QFileDialog::getOpenFileName(
this,
tr("Open File"),
tr("Text Files(*.txt)")
);
}
if(!path.isEmpty()) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(this, tr("Read File"),
tr("Cannot open file:\n%1").arg(path));
return;
}
QTextStream in(&file);
textEdit->setText(in.readAll());
file.close();
} else {
QMessageBox::warning(this, tr("Path"),
tr("You did not select any file."));
}
}