下面实现的是一个qt例子,有pro文件,main.cpp、mainwindow.cpp和mainwindow.h。
主要功能是mainWindow窗口上有个按钮1,要求设置按钮1的文本为“test”,点击弹出提示信息hello。
视觉效果上要求mainwindow首先设置为全屏无边框并且居前显示+全透明显示,就是不可见,按钮能看见,在窗口下面的其他应用还可以正常使用。
test1.pro文件代码如下:
QT += widgets
CONFIG += c++11
TARGET = example
TEMPLATE = app
SOURCES += main.cpp \
mainwindow.cpp
HEADERS += mainwindow.h
mainwindow.h代码如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QMessageBox>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
private:
QPushButton *m_button1;
private slots:
void showMessage();
};
#endif // MAINWINDOW_H
mainwindow.cpp代码如下:
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setStyleSheet("background-color: transparent;");
m_button1 = new QPushButton("test", this);
m_button1->setGeometry(100, 100, 200, 60);
m_button1->setStyleSheet("background-color: gray;");
connect(m_button1, &QPushButton::clicked, this, &MainWindow::showMessage);
}
void MainWindow::showMessage()
{
QMessageBox::information(this, "Message", "Hello");
}
main.cpp代码如下:
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
w.setAttribute(Qt::WA_TranslucentBackground, true);
w.showFullScreen();
return a.exec();
}
运行效果如下: