增加拖放上传
This commit is contained in:
parent
2742edd163
commit
4322e27ea6
|
@ -37,14 +37,14 @@ MainWindow::MainWindow(QWidget *parent) :
|
|||
connect(ui->pushButton_start, &QPushButton::toggled, [=](bool b){
|
||||
if (b) {
|
||||
state = 0;
|
||||
qint64 port = 9999;
|
||||
while (!tcpServer->listen(QHostAddress::Any, port)) {
|
||||
port--;
|
||||
port_local = 9999;
|
||||
while (!tcpServer->listen(QHostAddress::Any, port_local)) {
|
||||
port_local--;
|
||||
}
|
||||
ui->label_local->setText(IP + ":" + QString::number(port));
|
||||
ui->lineEdit_port->setText(QString::number(port));
|
||||
ui->label_local->setText(IP + ":" + QString::number(port_local));
|
||||
ui->lineEdit_port->setText(QString::number(port_local));
|
||||
ui->textBrowser->clear();
|
||||
ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] 监听:" + IP + ":" + QString::number(port));
|
||||
ui->textBrowser->append("[" + QDateTime::currentDateTime().toString("HH:mm:ss") + "] 监听:" + IP + ":" + QString::number(port_local));
|
||||
ui->pushButton_start->setText("Stop");
|
||||
} else {
|
||||
tcpServer->close();
|
||||
|
@ -155,6 +155,10 @@ QString MainWindow::BS(long b)
|
|||
|
||||
void MainWindow::upload(QString filepath)
|
||||
{
|
||||
if (ui->lineEdit_ip->text() == IP) {
|
||||
ui->statusBar->showMessage("不需要上传给自己!");
|
||||
return;
|
||||
}
|
||||
qint64 port = ui->lineEdit_port->text().toInt();
|
||||
QTcpSocket *tcpSocket1 = new QTcpSocket(this);
|
||||
connect(tcpSocket1, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){
|
||||
|
@ -199,4 +203,25 @@ void MainWindow::upload(QString filepath)
|
|||
tcpSocket2->close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void MainWindow::dragEnterEvent(QDragEnterEvent *e)
|
||||
{
|
||||
//if(e->mimeData()->hasFormat("text/uri-list")) //只接收文件列表
|
||||
e->acceptProposedAction(); //可以在这个窗口部件上拖放对象
|
||||
}
|
||||
|
||||
void MainWindow::dropEvent(QDropEvent *e) //释放对方时,执行的操作
|
||||
{
|
||||
QList<QUrl> urls = e->mimeData()->urls();
|
||||
qDebug() << urls << urls.size();
|
||||
if (urls.isEmpty())
|
||||
return;
|
||||
|
||||
foreach (QUrl url, urls) {
|
||||
qDebug() << url.toString();
|
||||
QString filepath = url.toLocalFile();
|
||||
qDebug() << filepath;
|
||||
upload(filepath);
|
||||
}
|
||||
}
|
|
@ -16,6 +16,9 @@
|
|||
#include <QMetaEnum>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QDropEvent>
|
||||
#include <QDragEnterEvent>
|
||||
#include <QMimeData>
|
||||
|
||||
namespace Ui {
|
||||
class MainWindow;
|
||||
|
@ -37,10 +40,12 @@ private:
|
|||
QTcpSocket *tcpSocket;
|
||||
int state;
|
||||
QFile file;
|
||||
qint64 length;
|
||||
qint64 length, port_local;
|
||||
QString BS(long b);
|
||||
void upload(QString filepath);
|
||||
double lastModified = 0;
|
||||
void dragEnterEvent(QDragEnterEvent*);
|
||||
void dropEvent(QDropEvent*);
|
||||
|
||||
private slots:
|
||||
void newConnect();
|
||||
|
|
|
@ -10,6 +10,9 @@
|
|||
<height>500</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="acceptDrops">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>文件传输</string>
|
||||
</property>
|
||||
|
@ -76,16 +79,27 @@
|
|||
</item>
|
||||
<item row="2" column="1" colspan="2">
|
||||
<widget class="QLineEdit" name="lineEdit_port">
|
||||
<property name="acceptDrops">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="1" colspan="2">
|
||||
<widget class="QLineEdit" name="lineEdit_root_dir"/>
|
||||
<widget class="QLineEdit" name="lineEdit_root_dir">
|
||||
<property name="acceptDrops">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QLineEdit" name="lineEdit_ip"/>
|
||||
<widget class="QLineEdit" name="lineEdit_ip">
|
||||
<property name="acceptDrops">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
|
|
Loading…
Reference in New Issue