新建 widget 项目可参考
QT入门初学者——如何新建一个工程项目(详细)_guuuuug的博客-CSDN博客_qt新建工程
实现界面,点击浏览会打开文件夹
选中文件,窗口显示文件名,文件路径
过长的文字会在最后显示...,鼠标悬浮在文字栏会显示全部文字。
点击右下角打开可以实现打开目标文件目录(0 ~ 4个),点击清除可以清除全部
代码实现:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QPushButton>
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QString>
#include <QFileDialog>
#include <QDialogButtonBox>
#include <QDesktopServices>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_lineEdit_textChanged( );
void on_lineEdit( );
private:
Ui::Widget *ui;
QPushButton *b1;
QLabel *label;
QLabel *label1;
QPushButton *b2;
QLabel *label2;
QLabel *label22;
QPushButton *b3;
QLabel *label3;
QLabel *label133;
QPushButton *b4;
QLabel *label4;
QLabel *label144;
QPushButton *b10;
QPushButton *b11;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
b1 = new QPushButton;
b2 = new QPushButton;
b3 = new QPushButton;
b4 = new QPushButton;
label = new QLabel;
label1 = new QLabel;
label2 = new QLabel;
label22 = new QLabel;
label3 = new QLabel;
label133 = new QLabel;
label4 = new QLabel;
label144 = new QLabel;
b1->setParent(this);
b1->setText("浏览");
b1->setGeometry(420,50,50,30);
label->setParent(this);
label->setGeometry(20,50,100,30);
label->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
label1->setParent(this);
label1->setGeometry(150,50,250,30);
label1->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
b2->setParent(this);
b2->setText("浏览");
b2->setGeometry(420,100,50,30);
label2->setParent(this);
label2->setGeometry(20,100,100,30);
label2->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
label22->setParent(this);
label22->setGeometry(150,100,250,30);
label22->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
b3->setParent(this);
b3->setText("浏览");
b3->setGeometry(420,150,50,30);
label3->setParent(this);
label3->setGeometry(20,150,100,30);
label3->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
label133->setParent(this);
label133->setGeometry(150,150,250,30);
label133->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
b4->setParent(this);
b4->setText("浏览");
b4->setGeometry(420,200,50,30);
label4->setParent(this);
label4->setGeometry(20,200,100,30);
label4->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
label144->setParent(this);
label144->setGeometry(150,200,250,30);
label144->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
b10 = new QPushButton;
b10->setParent(this);
b10->setGeometry(300, 255, 50, 20);
b10->setText("打开");
b11 = new QPushButton;
b11->setParent(this);
b11->setGeometry(380, 255, 50, 20);
b11->setText("清除");
//槽函数的两种写法
//connect(b1, SIGNAL(clicked()), this, SLOT(on_lineEdit_textChanged())); 写法1
connect(b1, &QPushButton::clicked, this, &Widget::on_lineEdit_textChanged); //写法2
connect(b2, &QPushButton::clicked, this, &Widget::on_lineEdit_textChanged);
connect(b3, &QPushButton::clicked, this, &Widget::on_lineEdit_textChanged);
connect(b4, &QPushButton::clicked, this, &Widget::on_lineEdit_textChanged);
b1->setObjectName("1");
b2->setObjectName("2");
b3->setObjectName("3");
b4->setObjectName("4");
connect(b10, &QPushButton::clicked, this, &Widget::on_lineEdit);
connect(b11, &QPushButton::clicked, this, &Widget::on_lineEdit);
b10->setObjectName("10");
b11->setObjectName("11");
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_lineEdit_textChanged( )
{
QString fullPath = QFileDialog::getOpenFileName();
QString filename, filePath;
QFileInfo fileInfo = QFileInfo(fullPath);
//文件名
filename = fileInfo.fileName();
//绝对路径l
filePath = fileInfo.absolutePath();
QFontMetrics fontWidth(label1->font());//得到每个字符的宽度
QString elideNote = fontWidth.elidedText(filePath, Qt::ElideRight, 250);//最大宽度250像素
QString fileName = fontWidth.elidedText(filename, Qt::ElideRight, 100);//最大宽度100像素
QPushButton* btn= qobject_cast<QPushButton*>(sender());
QString str = btn->objectName();
if(str == "1"){
label->setText(fileName);
label->setToolTip(filename);
label1->setText(elideNote);
label1->setToolTip(filePath);
}
if(str == "2"){
label2->setText(fileName);
label2->setToolTip(filename);
label22->setText(elideNote);
label22->setToolTip(filePath);
}
if(str == "3"){
label3->setText(fileName);
label3->setToolTip(filename);
label133->setText(elideNote);
label133->setToolTip(filePath);
}
if(str == "4"){
label4->setText(fileName);
label4->setToolTip(filename);
label144->setText(elideNote);
label144->setToolTip(filePath);
}
}
void Widget::on_lineEdit(){
QPushButton* btn= qobject_cast<QPushButton*>(sender());
QString str = btn->objectName();
if(str == "10"){
QString st= label1->toolTip();
QDesktopServices::openUrl(QUrl::fromLocalFile(st));
QString st1= label22->toolTip();
QDesktopServices::openUrl(QUrl::fromLocalFile(st1));
QString st2= label133->toolTip();
QDesktopServices::openUrl(QUrl::fromLocalFile(st2));
QString st3= label144->toolTip();
QDesktopServices::openUrl(QUrl::fromLocalFile(st3));
}
else if(str == "11"){
label->clear();
label1->clear();
label1->setToolTip(" ");
label2->clear();
label22->clear();
label22->setToolTip("");
label3->clear();
label133->clear();
label133->setToolTip("");
label4->clear();
label144->clear();
label144->setToolTip("");
}
}
main.cpp
#include "widget.h"
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QString>
#include <QFileDialog>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.resize(500,300);
w.show();
return a.exec();
}
推荐一下,这位大佬写的非常详细,还有源码可下载:
QT GUI编程_子羽丿的博客-CSDN博客
版权归原作者 糯米团wwww 所有, 如有侵权,请联系我们删除。