Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在QT中向小部件的角落添加按钮?_C++_Qt_Layout - Fatal编程技术网

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

C++ 如何在QT中向小部件的角落添加按钮?,c++,qt,layout,C++,Qt,Layout,我试图显示一个正方形的图像,并在右上角有一个X(一半在图像中,一半在外部)来关闭图像。我不知道有哪个版面经理会允许我这么做。我如何实现这一点 +---------O <- close button | | | | +---------+ +---------O这里有很多东西需要实施。我通过以下方式实现了这一点: 第一步。子类QLabel,使捕获鼠标点击成为可能。在标题中,声明单击并按下的信号,并覆盖正确的鼠标事件 LabelButton::LabelButt

我试图显示一个正方形的图像,并在右上角有一个X(一半在图像中,一半在外部)来关闭图像。我不知道有哪个版面经理会允许我这么做。我如何实现这一点

+---------O <- close button | | | | +---------+
+---------O这里有很多东西需要实施。我通过以下方式实现了这一点:

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

LabelButton::LabelButton(QWidget *parent) : QLabel(parent)
{
}
void LabelButton::mouseReleaseEvent(QMouseEvent *event){
    emit Clicked();
    event->accept();
}
void LabelButton::mousePressEvent(QMouseEvent *event){
    emit Pressed();
    event->accept();
}
第二步。将一个名为xbutton的LabelButton(包含一个圆形“x”图像)添加到您想要的小部件位置

示例(这将在setupUi函数中):

第三步。创建你的小部件。将其背景设置为透明,并确保其大小包括“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按钮时,窗口将关闭。但在实现之前,您将不会有正常的移动功能

第四步。实现小部件的移动功能

/*
 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());
    }
}
我发现这种方式对我最有用,因为我需要从定制的外观光滑的小部件中获得很多功能


我真的很喜欢创造性的用户界面,我希望你的界面完成后看起来真的很光滑

如果你被困在逻辑上,它会是这样的:创建一个无边框画布,大小为基本图像+X图像的一半。将基础图像放置在左下角。将X图像放置在基础图像顶部的右上角。
/*
 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());
    }
}