Qt拖拽功能
1.Qt拖拽功能
Qt拖拽功能
窗口拖拽文件读取文件路径+文件名
void MainWindow::dragEnterEvent(QDragEnterEvent *ev)
{
if(ev->mimeData()->hasUrls())
{
ev->accept();//接收拖动进入事件
}
//ev->ignore();//若忽略该事件,则不会发生之后的事件,拖放到此结束
}
void MainWindow::dropEvent(QDropEvent *ev)
{
QList<QUrl> urls = ev->mimeData()->urls();
ui->lineEdit_source->setText(urls[0].toLocalFile());
ui->lineEdit_target->setText(urls[0].toLocalFile());
//if(ev->mimeData()->hasUrls())
//{
//QList<QUrl> urls = ev->mimeData()->urls();
// ui->lineEdit_source->text() = urls.at(0).toLocalFile();
// ui->lineEdit_target->text() = urls.at(1).toLocalFile();
//for(int i=0; i<urls.size(); i++)
//{
//}
//}
}
调试过程信号槽函数
ui->label->setAcceptDrops(true);
拖拽到指定控件获得路径+文件名
https://www.pianshen.com/article/872738975/
bool eventFilter(QObject *watched, QEvent *event);
//drag file
ui->pushButton->installEventFilter(this);
ui->pushButton->setAcceptDrops(true);
ui->pushButton_2->installEventFilter(this);
ui->pushButton_2->setAcceptDrops(true);
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if (watched == ui->pushButton)
{
if (event->type() == QEvent::DragEnter) {
// [[2]]: 当拖放时鼠标进入label时, label接受拖放的动作
QDragEnterEvent *dee = dynamic_cast<QDragEnterEvent *>(event);
dee->acceptProposedAction();
return true;
} else if (event->type() == QEvent::Drop)
{
QDropEvent *de = dynamic_cast<QDropEvent *>(event);
QList<QUrl> urls = de->mimeData()->urls();
if (urls.isEmpty()) { return true; }
ui->lineEdit_source->setText( urls.first().toLocalFile());;
}
} else if (watched == ui->pushButton_2)
{
if (event->type() == QEvent::DragEnter)
{
// [[2]]: 当拖放时鼠标进入label时, label接受拖放的动作
QDragEnterEvent *dee = dynamic_cast<QDragEnterEvent *>(event);
dee->acceptProposedAction();
return true;
} else if (event->type() == QEvent::Drop)
{
// [[3]]: 当放操作发生后, 取得拖放的数据
QDropEvent *de = dynamic_cast<QDropEvent *>(event);
QList<QUrl> urls = de->mimeData()->urls();
if (urls.isEmpty()) { return true; }
ui->lineEdit_target->setText( urls.first().toLocalFile());
}
}
return QMainWindow::eventFilter(watched, event);
}
显示当前时间
//view time
void MainWindow::slotTimerUpdate()
{
QDateTime time = QDateTime::currentDateTime();//获取当前日期和时间
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");//格式为年-月-日 小时-分钟-秒 星期
MainWindow::labDateTime->setText(str);
}
发射信号
QTimer *timer = new QTimer(this);
timer->start(1000);
接收信号
connect(timer,SIGNAL(timeout()),this,SLOT(slotTimerUpdate()));
Qt背景图片切换
//ui->change_background_btn->installEventFilter(this);
//ui->change_background_btn->setAcceptDrops(true);
可不加上面代码
void MainWindow::paintEvent(QPaintEvent *event)
{
static int pos=0;
QString picture[2]={":/image/1.jpg",":/image/2.jpg"};
int n=sizeof(picture)/sizeof(picture[0]);
QPainter* painter=new QPainter(this);
//draw picture
QPixmap pixmap;
pixmap.load(picture[pos]);
pixmap.scaled(this->width(),this->height());
painter->drawPixmap(0,0,this->width(),this->height(),pixmap);
//图片切换
//pos=!pos;
pos++;
if(pos==n)
{
pos=0;
}
}
信号槽连接
connect(ui->change_background_btn,&QPushButton::clicked,[=](){MainWindow::update();});
Qt配置mysql
https://blog.csdn.net/tianpenglengyue/article/details/120437305
https://blog.csdn.net/joey_ro/article/details/105411135
第一个配置后不行,然后第二个配置好的,先试一试第二个配置。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术