2012-02-21 72 views
1

我想显示一个正方形的图像,并在右上角有一个X(一半在图像的一半以外)来关闭图像。我不知道布局经理会允许我这样做。我如何实现这一点?如何将按钮添加到QT中的小部件的角落?

 
+---------O <- close button 
|   | 
|   | 
+---------+ 

+0

如果你被困在逻辑上,它会像这样:创建一个无边框画布,大小为基本图像+ X图像大小的一半。将基本图像放置在左下方。将X图像放在基本图像顶部,位于右上角。 – 2012-02-21 20:20:23

回答

3

这里会有很多实现。我已经通过以下方式实现了这一点:

第1步。子类QLabel使捕获鼠标点击成为可能。在标题中声明信号Clicked和Pressed,并覆盖正确的鼠标事件。

LabelButton::LabelButton(QWidget *parent) : QLabel(parent) 
{ 
} 
void LabelButton::mouseReleaseEvent(QMouseEvent *event){ 
    emit Clicked(); 
    event->accept(); 
} 
void LabelButton::mousePressEvent(QMouseEvent *event){ 
    emit Pressed(); 
    event->accept(); 
} 

第2步:添加一个名为xbutton在你想要的位置包含圆形“X”形象窗口小部件的LabelButton。

示例(这将是你的setupUi功能):

... 
xbutton = new LabelButton(MainWidget); 
xbutton->setObjectName(QString::fromUtf8("xbutton")); 
xbutton->setGeometry(QRect(0, 0, 31, 31)); 
xbutton->setPixmap(QPixmap(QString::fromUtf8(":/xbutton.gif"))); 
xbutton->setAlignment(Qt::AlignCenter); 
... 

第3步:创建你的widget。将其背景设置为透明,并确保其大小包含'x'关闭按钮的空间。注意:将您的背景设置为透明意味着您的小部件必须包含一些接受用户输入的子部件。

例子:

mywidget::mywidget(QWidget *parent): QWidget(parent){ 
    setupUi(this); 
    moving=false; // notice that you must declare this bool for Step 4. 
    offset=QPoint(0,0); // Also a QPoint for Step 4 
#if defined(Q_WS_MAC) //These values worked for me with the Mac OS 10.5 SDK 
    this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint | Qt::Window); 
    QPalette pal = this->palette(); 
    pal.setColor(this->backgroundRole(), Qt::transparent); 
    this->setPalette(pal); 
#elif defined(Q_WS_WIN)//These values worked for me on Windows XP/Vista/7 
    this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint |Qt::FramelessWindowHint | Qt::Window); 
    setStyleSheet("background:transparent;"); 
    setAttribute(Qt::WA_TranslucentBackground); 
#endif 
    connect(xbutton,SIGNAL(Clicked()),this,SLOT(hide())); 
} 

现在你有你最初期望的功能。当你点击x按钮时,窗口将关闭。但是,除非你实现这个功能,否则你不会有正常的移动功能

第4步。实现移动功能到您的小部件。

/* 
FUNCTION:mousePressEvent 
used to help move the widget since there is no title bar, sets the initial offset of the mouse 
*/ 
void mywidget::mousePressEvent(QMouseEvent *event){ 
    if((event->button() == Qt::LeftButton)) { 
     moving = true; 
     offset = event->globalPos() - this->pos(); 
    } 
} 
/* 
FUNCTION:mouseReleaseEvent 
used to help move the widget since there is no title bar, releases the "moving" attribute 
*/ 
void mywidget::mouseReleaseEvent(QMouseEvent *event){ 
    if(event->button() == Qt::LeftButton) { 
     moving = false; 
    } 
} 
/* 
FUNCTION:mouseMoveEvent 
used to help move the widget since there is no title bar 
*/ 
void mywidget::mouseMoveEvent(QMouseEvent *event){ 
    if(moving){ 
    QPoint global = event->globalPos(); 
    this->setGeometry(global.x()-offset.x(),global.y()-offset.y(),this->width(),this->height()); 
    } 
} 

我发现这种方式是最对我有用处,因为我需要很多功能我的个性圆滑的窗口设计的。

我真的很喜欢创造性的用户界面,我希望你看起来非常时尚,当你完成!

+0

哇,非常感谢! – chacham15 2012-02-21 20:50:50

+0

没问题,如果您的用户从未获得特殊的“x关闭”功能,那将是一件耻辱。他们会反抗! – buster 2012-02-21 20:56:38

相关问题