widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QCommandLinkButton>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private:
QWidget *w;
QCommandLinkButton *cmmBtn;
private slots:
void clickFunc();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QLabel>
#include <QVBoxLayout>
Widget::Widget(QWidget *parent) :
QWidget(parent),
w(nullptr),
ui(new Ui::Widget)
{
cmmBtn = new QCommandLinkButton("Version", "Version project", this);
cmmBtn->setFlat(true);
connect(cmmBtn, SIGNAL(clicked()), this, SLOT(clickFunc()));
ui->setupUi(this);
}
void Widget::clickFunc()
{
if(w)
{
w->close();
delete w;
}
w = new QWidget();
w->setWindowFlags(Qt::CustomizeWindowHint|Qt::WindowType_Mask);
QLabel *label = new QLabel("QCommandLinkButton Click.", w);
QVBoxLayout *layout = new QVBoxLayout();
w->setGeometry(this->pos().x()+this->width()/2-100,
this->pos().y()+this->height()/2-50,
200,
100);
layout->addWidget(label, 0, Qt::AlignCenter);
w->setLayout(layout);
w->show();
}
Widget::~Widget()
{
if(w)
{
w->close();
delete w;
}
delete ui;
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
运行结果: