简体   繁体   English

如何在QT中向窗口小部件的角落添加按钮?

[英]How can I add a button to the corner of a widget in QT?

I am trying to display a square image and have an X in the top right corner (half in the image half outside) to close the image. 我试图显示一个方形图像,并在右上角有一个X(图像的一半在外面),以关闭图像。 I dont know of a layout manager that will allow me to do that. 我不知道会允许我这样做的布局管理器。 How do I implement this? 我该如何实现?

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

There will be a lot to implement here. 这里将有很多实现。 I've accomplished this in the following way: 我是通过以下方式完成此操作的:

Step 1. Subclass QLabel to make it possible to capture mouse clicks. 步骤1.子类QLabel使捕获鼠标点击成为可能。 In the header declare signals Clicked and Pressed, and override the proper mouse events. 在标题中声明信号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();
}

Step 2. Add a LabelButton called xbutton containing an circular 'x' image to your widget at the location that you desire. 第2步。在所需位置添加一个名为xbutton的LabelButton,其中包含圆形“ x”图像。

Example (This would be in your setupUi function): 示例(这将在您的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);
...

Step 3. Create your widget. 第3步。创建小部件。 Set its background to transparent, and make sure its size includes room for the 'x' close button. 将其背景设置为透明,并确保其大小包含用于“ x”关闭按钮的空间。 Note: Setting your background to transparent means your widget will have to contain some child widget that accepts input from the user. 注意:将背景设置为透明意味着您的窗口小部件必须包含一些接受用户输入的子窗口小部件。

Example: 例:

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()));
}

Now you have the functionality that you originally desired. 现在您拥有了最初所需的功能。 When you click the xbutton, the window will close. 单击x按钮时,窗口将关闭。 But you will not have normal move functionality until you implement that. 但在实现之前,您将无法获得正常的移动功能。

Step 4. Implement move functionality to your widget. 步骤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());
    }
}

I found this way to be of most use to me, because I needed lots of functionality out of my customized slick-looking widget. 我发现这种方式对我来说最有用,因为我需要从我定制的光滑外观小部件中获得许多功能。

I really enjoy creative user interfaces and I hope that yours looks really sleek when you get it finished! 我真的很喜欢创意的用户界面,希望您完成后看起来真的很时尚!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM