无框和透明窗口qt5 [英] Frameless and transparent window qt5
问题描述
我对qt和c ++很新,我遇到了一个问题,我似乎无法想象。我想打开一个无框和透明的窗口,当我单击主ui上的按钮。我有代码工作,打开一个新的窗口,当我按下主要ui上的按钮,但我似乎无法获得无框和透明部分工作。
main.cpp
#includelearnwindow.h
#include< QApplication>
int main(int argc,char * argv [])
{
QApplication a(argc,argv);
LearnWindow w;
w.show();
return a.exec();
}
这里是LearnWindow.h
#ifndef LEARNWINDOW_H
#define LEARNWINDOW_H
#include< QMainWindow>
#include< transwindow.h>
namespace Ui {
class LearnWindow;
}
class LearnWindow:public QMainWindow
{
Q_OBJECT
public:
explicit LearnWindow(QWidget * parent = 0 );
〜LearnWindow();
私有插槽:
void on_pushButton_clicked();
private:
Ui :: LearnWindow * ui;
TransWindow * winTrans;
public slots:
void openTrans();
};
#endif // LEARNWINDOW_H
这里是learnwindow.cpp
#includelearnwindow.h
#includeui_learnwindow.h
LearnWindow :: LearnWindow(QWidget * parent):
QMainWindow(parent),
ui(new Ui :: LearnWindow)
{
ui-> setupUi(this);
}
LearnWindow ::〜LearnWindow()
{
delete ui;
}
void LearnWindow :: openTrans()
{
winTrans = new TransWindow(this);
// winTrans-> setWindowTitle(NewWin);
// winTrans-> setWindowFlags(Qt :: FramelessWindowHint | Qt :: X11BypassWindowManagerHint);
// winTrans-> setAttribute(Qt :: WA_TranslucentBackground,true);
// winTrans-> setAutoFillBackground(false);
// winTrans-> setStyleSheet(background:transparent;);
winTrans-> show();
}
void LearnWindow :: on_pushButton_clicked()
{
openTrans();
}
这里是transwindow.h
#ifndef TRANSWINDOW_H
#define TRANSWINDOW_H
#include< QDialog>
命名空间Ui {
class TransWindow;
}
类TransWindow:public QDialog
{
Q_OBJECT
public:
显式TransWindow(QWidget * parent = 0 );
// setWindowFlags(windowFlags()| Qt :: FramelessWindowHint);
〜TransWindow();
private:
Ui :: TransWindow * ui;
};
#endif // TRANSWINDOW_H
这里是transwindow.cpp
#includetranswindow.h
#includeui_transwindow.h
TransWindow: :TransWindow(QWidget * parent):
QDialog(parent),
ui(new Ui :: TransWindow)
{
// setWindowTitle(NewWin);
// setWindowFlags(Qt :: FramelessWindowHint);
// setAttribute(Qt :: WA_TranslucentBackground,true);
ui-> setupUi(this);
}
TransWindow ::〜TransWindow()
{
delete ui;
}
在不同的源代码中,您会看到注释掉的行,我试过的东西。大多数情况下,问题是,如果我取消注释任何行试图设置Qt :: FramlessWindowHint的程序正常运行,但从未打开一个新的窗口,当我单击主ui上的按钮。 / p>
如果我取消注释任何设置Qt :: WA_TranslucentBackground的行,新窗口将打开时,当按钮在主ui但是新窗口的背景是黑色的,不透明。
其他可能相关的信息:
linux:ubunto 12.04
qt 5.0。 2(64位)
qt creator 2.7.1
像我前面所说的,我在这里很新, m缺失,使这项工作正常。
尝试:
setWindowFlags(Qt :: Widget | Qt :: FramelessWindowHint);
setParent(0); // Create TopLevel-Widget
setAttribute(Qt :: WA_NoSystemBackground,true);
setAttribute(Qt :: WA_TranslucentBackground,true);
setAttribute(Qt :: WA_PaintOnScreen); //不需要在Qt 5.2及以上
I'm quite new to qt and c++ and I've encountered a problem that I can't seem to figure out. I'm wanting to open a frameless and transparent window when I click a button on the main ui. I've got the code working to open a new window when I press a button on the main ui but I can't seem to get the frameless and transparent part working.
Here is the source codes for the small program that I wrote to learn this
main.cpp
#include "learnwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
LearnWindow w;
w.show();
return a.exec();
}
Here is the LearnWindow.h
#ifndef LEARNWINDOW_H
#define LEARNWINDOW_H
#include <QMainWindow>
#include <transwindow.h>
namespace Ui {
class LearnWindow;
}
class LearnWindow : public QMainWindow
{
Q_OBJECT
public:
explicit LearnWindow(QWidget *parent = 0);
~LearnWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::LearnWindow *ui;
TransWindow *winTrans;
public slots:
void openTrans();
};
#endif // LEARNWINDOW_H
Here is learnwindow.cpp
#include "learnwindow.h"
#include "ui_learnwindow.h"
LearnWindow::LearnWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::LearnWindow)
{
ui->setupUi(this);
}
LearnWindow::~LearnWindow()
{
delete ui;
}
void LearnWindow::openTrans()
{
winTrans = new TransWindow (this);
//winTrans->setWindowTitle("NewWin");
// winTrans->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
//winTrans->setAttribute(Qt::WA_TranslucentBackground,true);
//winTrans->setAutoFillBackground(false);
//winTrans->setStyleSheet("background:transparent;");
winTrans->show();
}
void LearnWindow::on_pushButton_clicked()
{
openTrans();
}
Here is the transwindow.h
#ifndef TRANSWINDOW_H
#define TRANSWINDOW_H
#include <QDialog>
namespace Ui {
class TransWindow;
}
class TransWindow : public QDialog
{
Q_OBJECT
public:
explicit TransWindow(QWidget *parent = 0);
//setWindowFlags(windowFlags()| Qt::FramelessWindowHint);
~TransWindow();
private:
Ui::TransWindow *ui;
};
#endif // TRANSWINDOW_H
And here is transwindow.cpp
#include "transwindow.h"
#include "ui_transwindow.h"
TransWindow::TransWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::TransWindow)
{
//setWindowTitle("NewWin");
//setWindowFlags(Qt::FramelessWindowHint);
//setAttribute(Qt::WA_TranslucentBackground,true);
ui->setupUi(this);
}
TransWindow::~TransWindow()
{
delete ui;
}
In the different source codes you'll see commented out lines which is the different things that I've tried. For the most part the problem is, if I un-comment out any of the lines that try to set the "Qt::FramlessWindowHint" the program runs normally but never opens a new window when I click the button on the main ui.
If I un-comment out any of the lines where I set the "Qt::WA_TranslucentBackground" the new window will open up when when the button is pressed in the main ui but the background of the new window is black, not transparent.
Other info that may be pertinent: linux: ubunto 12.04 qt 5.0.2 (64-bit) qt creator 2.7.1
Like I stated earlier I'm quite new at this and don't understand exactly what I'm missing to make this work properly. Any assistance anyone can provide would be greatly appreciated.
Try this:
setWindowFlags(Qt::Widget | Qt::FramelessWindowHint);
setParent(0); // Create TopLevel-Widget
setAttribute(Qt::WA_NoSystemBackground, true);
setAttribute(Qt::WA_TranslucentBackground, true);
setAttribute(Qt::WA_PaintOnScreen); // not needed in Qt 5.2 and up
这篇关于无框和透明窗口qt5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!