本次学习了拖放文件相关内容,是紧接着上一篇文章的,这次学习主要参考了博文:跟我一起学QT7:拖放文件,地址:https://my.oschina.net/voler/blog/345722
//dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QListWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
public slots:
void AToB_clicked();
void BToA_clicked();
private:
void moveCurrentItem(QListWidget *source,
QListWidget *target);
private:
QListWidget *listA;
QListWidget *listB;
QPushButton *buttonAToB;
QPushButton *buttonBToA;
};
#endif // DIALOG_H
//dialo.cpp
#include "dialog.h"
Dialog::Dialog(QWidget *parent) :
QDialog(parent)
{
listA = new QListWidget;
listB = new QListWidget;
buttonAToB = new QPushButton("->");
buttonBToA = new QPushButton("<-");
listA->addItem("Giosue Carducci");
listA->addItem("Eyvind Johnson");
listA->addItem("Sally Prudhomme");
listA->addItem("Henryk Sienkiewicz");
listA->addItem("Carl Spitteler");
listA->addItem("Rabindranath Tagore");
listA->addItem("Kawabata Yasunari");
listB->addItem("Rudolf Eucken");
listB->addItem("Anatole France");
listB->addItem("Rudyard Kipling");
listB->addItem("Thomas Mann");
listB->addItem("Eugene O'Neill");
listB->addItem("Sigrid Undset");
buttonAToB->setText("->");
buttonBToA->setText("<-");
QVBoxLayout *buttonLayout = new QVBoxLayout;
buttonLayout->addWidget(buttonAToB);
buttonLayout->addWidget(buttonBToA);
QHBoxLayout *hboxLayout = new QHBoxLayout;
hboxLayout->addWidget(listA);
hboxLayout->addLayout(buttonLayout);
hboxLayout->addWidget(listB);
setLayout(hboxLayout);
//设置窗口的固定高度和宽度
setFixedHeight( sizeHint().height() );
connect(buttonAToB, SIGNAL(clicked()),
this, SLOT(AToB_clicked()));
connect(buttonBToA, SIGNAL(clicked()),
this, SLOT(BToA_clicked()));
}
Dialog::~Dialog()
{
}
//把数据移到右边窗口
void Dialog::AToB_clicked()
{
moveCurrentItem(listA, listB);
}
//把数据移到左边窗口
void Dialog::BToA_clicked()
{
moveCurrentItem(listB, listA);
}
void Dialog::moveCurrentItem(QListWidget *source,
QListWidget *target)
{
if (source->currentItem()) {
QListWidgetItem *newItem = source->currentItem()->clone();
target->addItem(newItem);
target->setCurrentItem(newItem);
delete source->currentItem();
}
}
//main.cpp
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}