定制形状的QWidgets消失了

时间:2013-06-11 12:50:25

标签: qt qt4 widget qwidget

我没有成功修改Qt的WindowFlags示例以获得自定义Widget。起初看起来很容易,但我无法弄清楚它为什么不起作用。

示例的添加在PreviewWindow类的构造函数中:

QVector<QPoint> pts;

pts.push_back(QPoint(0, 10));
pts.push_back(QPoint(36, 10));
pts.push_back(QPoint(36+10, 0));
pts.push_back(QPoint(36+20, 10));
pts.push_back(QPoint(296, 10));
pts.push_back(QPoint(296, 266));
pts.push_back(QPoint(0, 266));

QPolygon p(pts);

setMask(QRegion(pts));

现在,当我使用此添加运行示例时,我看不到一个漂亮的窗口。但是,这不是我想要的:那些坐标仅指 Qt :: Tool 类型的窗口。

形状可以作为工具(只是尝试),但现在我想摆脱窗框。不幸的是,整个窗口绝望地消失了。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

我不知道为什么会这样,但现在就是这样。

首先,在设置标志后,小部件将消失,但我很放松,因为控制器窗口中的代码在设置标志(PreviewWindow::show())后立即调用ControllerWindow::updatePreview()

但是,我们需要在PreviewWindow类中设置标志后立即调用show ,如下所示:

void PreviewWindow::setWindowFlags(Qt::WindowFlags flags)
{
    QWidget::setWindowFlags(flags);

    // This is necessary to show the window again
    show();

    QString text;

    Qt::WindowFlags type = (flags & Qt::WindowType_Mask);
    if (type == Qt::Window) {
        text = "Qt::Window";
    } else if (type == Qt::Dialog) {
        text = "Qt::Dialog";
    } else if (type == Qt::Sheet) {
        text = "Qt::Sheet";
    } else if (type == Qt::Drawer) {
        text = "Qt::Drawer";
    } else if (type == Qt::Popup) {
        text = "Qt::Popup";
    } else if (type == Qt::Tool) {
        text = "Qt::Tool";
    } else if (type == Qt::ToolTip) {
        text = "Qt::ToolTip";
    } else if (type == Qt::SplashScreen) {
        text = "Qt::SplashScreen";
    }

    if (flags & Qt::MSWindowsFixedSizeDialogHint)
        text += "\n| Qt::MSWindowsFixedSizeDialogHint";
    if (flags & Qt::X11BypassWindowManagerHint)
        text += "\n| Qt::X11BypassWindowManagerHint";
    if (flags & Qt::FramelessWindowHint)
        text += "\n| Qt::FramelessWindowHint";
    if (flags & Qt::WindowTitleHint)
        text += "\n| Qt::WindowTitleHint";
    if (flags & Qt::WindowSystemMenuHint)
        text += "\n| Qt::WindowSystemMenuHint";
    if (flags & Qt::WindowMinimizeButtonHint)
        text += "\n| Qt::WindowMinimizeButtonHint";
    if (flags & Qt::WindowMaximizeButtonHint)
        text += "\n| Qt::WindowMaximizeButtonHint";
    if (flags & Qt::WindowCloseButtonHint)
        text += "\n| Qt::WindowCloseButtonHint";
    if (flags & Qt::WindowContextHelpButtonHint)
        text += "\n| Qt::WindowContextHelpButtonHint";
    if (flags & Qt::WindowShadeButtonHint)
        text += "\n| Qt::WindowShadeButtonHint";
    if (flags & Qt::WindowStaysOnTopHint)
        text += "\n| Qt::WindowStaysOnTopHint";
    if (flags & Qt::CustomizeWindowHint)
        text += "\n| Qt::CustomizeWindowHint";

    textEdit->setPlainText(text);
}

所以,一条简单的线,放在一个奇怪的地方。有关为何发生这种情况的任何提示?