1、
- 首先说一些,如果是mainwindow的QT工程,如果打算做自己手写ui 界面的话,该如何将自己写的内容添加到mainwindow界面呢?
- 方法为: 新建一个widget类,然后将所有零件都用布局布置好后,只需将总布局添加到widet,然后令
this->setCentralWidget(widget)
即可
- 此次的Ui 界面就是自己写的,然后效果图是下面那个鬼样子
#include
#include
#include
#include
#include
#include
#include
#include
private:
Ui::MainWindow *ui;
QWidget *widget=new QWidget();
QLabel *sizeLabel=new QLabel("size");
QLabel *creatLabel=new QLabel("creat time");
QLabel *lastEditLabel=new QLabel("last open time");
QLabel *isDirLabel=new QLabel("is Dir?");
QHBoxLayout *h1=new QHBoxLayout;
QHBoxLayout *h2=new QHBoxLayout;
QVBoxLayout *v1=new QVBoxLayout;
QVBoxLayout *v2=new QVBoxLayout;
QVBoxLayout *v3=new QVBoxLayout;
QPushButton *exitButton=new QPushButton("exit");
QPushButton *readButton=new QPushButton("read file");
QLineEdit *sizeLineEdit=new QLineEdit;
QLineEdit *creatLineEdit=new QLineEdit;
QLineEdit *lastLineEdit=new QLineEdit;
QLineEdit *isDirLineEdit=new QLineEdit ;
private slots:
void readFileInfo();
};
#include
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
v1->addWidget(sizeLabel);
v1->addWidget(creatLabel);
v1->addWidget(lastEditLabel);
v1->addWidget(isDirLabel);
v2->addWidget(sizeLineEdit);
v2->addWidget(creatLineEdit);
v2->addWidget(lastLineEdit);
v2->addWidget(isDirLineEdit);
h1->addLayout(v1);
h1->addLayout(v2);
h2->addWidget(readButton);
h2->addWidget(exitButton);
v3->addLayout(h1);
v3->addLayout(h2);
widget->setLayout(v3);
this->setCentralWidget(widget);
QObject::connect(readButton,SIGNAL(clicked()),this,SLOT(readFileInfo()));
QObject::connect(exitButton,SIGNAL (clicked()),this,SLOT(close()));
}
void MainWindow::readFileInfo()
{
QString name=QFileDialog::getOpenFileName(this,"Open File",QDir::currentPath());
QFileInfo info(name);
sizeLineEdit->setText(QString::number(info.size()));
creatLineEdit->setText(info.birthTime().toString());
lastLineEdit->setText(info.lastRead().toString());
if(info.isDir())
{
isDirLineEdit->setText("Yes");
}
else {
isDirLineEdit->setText("No");
}
}
- 获得文件名需要
QFileDialog
类;
- 利用
QFileInfo
类来打开文件,并获得其所有可能的信息。
info.size()
返回long long int型的数字。需要借助QString的number全局函数;
info.birthTIme()
返回QDateTime
类型,需要借助toString()
函数;
info.lastRead()
返回QDateTime
类型,需要借助toString()
函数;
info.isDIr()
返回是否是文件夹的布尔类型值。