一个剪切板的代码
//*************************BEGIN
#include
#include
#include
#include
class My : public QWidget
{
public:
My();
private:
QMultiLineEdit *medit1;
QMultiLineEdit *medit2;
QPushButton *copy1, *cut1, *paste1;
QPushButton *copy2, *cut2, *paste2;
};
My::My()
{
resize(300,300);
copy1=new QPushButton("Copy",this);
copy1->setGeometry(10,5,40,40);
cut1=new QPushButton ("Cut",this);
cut1->setGeometry(55,5,40,40);
paste1=new QPushButton("Paste",this);
paste1->setGeometry(100,5,40,40);
copy2=new QPushButton("Copy",this);
copy2->setGeometry(160,5,40,40);
cut2=new QPushButton ("Cut",this);
cut2->setGeometry(205,5,40,40);
paste2=new QPushButton("Paste",this);
paste2->setGeometry(250,5,40,40);
medit1=new QMultiLineEdit(this);
medit1->setGeometry(5,50,140,245);
medit2=new QMultiLineEdit(this);
medit2->setGeometry(155,50,140,245);
connect(copy1,SIGNAL(clicked()),medit1,SLOT(copy()));
connect(cut1,SIGNAL(clicked()),medit1,SLOT(cut()));
connect(paste1,SIGNAL(clicked()),medit1,SLOT(paste()));
connect(copy2,SIGNAL(clicked()),medit2,SLOT(copy()));
connect(cut2,SIGNAL(clicked()),medit2,SLOT(cut()));
connect(paste2,SIGNAL(clicked()),medit2,SLOT(paste()));
}
int main(int argc, char** argv)
{
QApplication a(argc,argv);
My w;
a.setMainWidget(&w);
w.show();
return a.exec();
}
//____________________END
这里的复制,cut paste 键,都是和各自的edit相连,
是QLineEdit 里的函数。。
一个实现拖放功能的代码:
//*********************BEGIN
#include
#include
#include
#include
class MyDragSite:public QMultiLineEdit
{
public:
MyDragSite(QWidget *parent);
protected:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
};
void MyDragSite::mousePressEvent(QMouseEvent *event)
{
QMultiLineEdit::mousePressEvent(event);
}
void MyDragSite::mouseMoveEvent(QMouseEvent *event)
{
QTextDrag *drag=new QTextDrag(text(),this);
drag->dragCopy();
QMultiLineEdit::mouseMoveEvent(event);
}
MyDragSite::MyDragSite(QWidget *parent):QMultiLineEdit(parent)
{
}
class MyDropSite:public QMultiLineEdit
{
public:
MyDropSite(QWidget *parent);
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
};
void MyDropSite::dragEnterEvent(QDragEnterEvent *event)
{
if(QTextDrag::canDecode(event))
{
if(QTextDrag::canDecode(event))
event->accept();
}
}
void MyDropSite::dropEvent(QDropEvent *event)
{
QString text;
if(QTextDrag::decode(event,text))
{
int row,col;
getCursorPosition(&row,&col);
insertAt(text,row,col);
}
}
MyDropSite::MyDropSite(QWidget *parent):QMultiLineEdit(parent)
{}
int main(int argc, char **argv)
{
QApplication a(argc,argv);
QWidget w;
w.resize(500,500);
MyDragSite dragsite(&w);
dragsite.setGeometry(10,10,100,240);
/*
MyDropSite dropsite(&w);
dropsite.setGeometry(125,10,100,240);
*/ //这两行屏蔽可以撤掉。。。。
MyDragSite dragsite2(&w);
dragsite2.setGeometry(240,10,100,240);
a.setMainWidget(&w);
w.show();
return a.exec();
}
//_____________________END
当在一个对象上按左键时,mousePressedEvent()被调用,当然要是你想在这个时候做点别的事,就可以在它管辖的区域内加上相应的代码。
左键按下,移动鼠标时,mouseMoveEvent()被调用
光标定位到接收对象上时,dargEnterEvent() 被调用。
dragMoveEvent() 见QWidget部分
书上说这里涉及到一个解码的过程 ,不是很理解。
QProcess的一个例子:
#include
#include
#include
#include
#include
#include
class Process:public QWidget
{
public:
Process();
QProcess *pro;
};
Process::Process()
{
QStringList qsl;
// QString str="./noon";
// pro= new QProcess(str);
qsl.append( "./c" );
qsl+="162.105.74.15";
pro= new QProcess(qsl);
QDir dir("/home/wujin");
pro->setWorkingDirectory(dir);
if(pro->start()) printf("ok\n");
else printf("something is wrong\n");
}
int main(int argc, char **argv)
{
QApplication a(argc,argv);
Process w;
a.setMainWidget(&w);
w.show();
return a.exec();
}
阅读(1280) | 评论(0) | 转发(1) |