Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.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++ 如何在QGridLayout中将QWidget的背景置于其他Widget之下_C++_Qt_Background Image_Qwidget_Qlabel - Fatal编程技术网

C++ 如何在QGridLayout中将QWidget的背景置于其他Widget之下

C++ 如何在QGridLayout中将QWidget的背景置于其他Widget之下,c++,qt,background-image,qwidget,qlabel,C++,Qt,Background Image,Qwidget,Qlabel,我花了好几个小时试图理解(谷歌搜索)为什么我的小部件没有显示在一个QWidget中,我用它作为一个窗口 孩子们大多是QLabel和一些Qslider。该布局是QGridlayout 这是我的一些窗口代码: FenPrincipale::FenPrincipale() { this->setWindowTitle("Premier Test"); this->resize(400, 200); QPalette palette;

我花了好几个小时试图理解(谷歌搜索)为什么我的小部件没有显示在一个QWidget中,我用它作为一个窗口

孩子们大多是QLabel和一些Qslider。该布局是QGridlayout

这是我的一些窗口代码:

FenPrincipale::FenPrincipale()
    {
        this->setWindowTitle("Premier Test");
        this->resize(400, 200);
        QPalette palette;
        palette.setBrush(this->backgroundRole(), QBrush(QImage("images/metal400x200.png")));

        this->setPalette(palette);
        /*
        this->setStyleSheet("background-image: url(images/metal400x200.png); "
                            "background-position: top left; "
                            "background-repeat: repeat-xy;"
                            );
        */

        buildChildren();
        buildLayout();

        this->setLayout(layout_principale);
    }
编辑:

我的孩子们是如何成长的

void FenPrincipale::buildChildren()
{
    m_title = new QLabel("MonTest");
    m_nextPageButton = new QLabel(">");

    m_line = new QLabel("Slide here");
    m_labelSlider = new QSlider;
    m_result = new QLabel("Result");

    /* 
     * Other children is set here...
    */
}
瞧,结果如何:

我尝试更改字体和其他方法来设置背景,但背景总是显示在其他小部件上


我遗漏了什么吗?

看起来您已经接近样式表实现了。我通过以下方法得到正确的结果:

setStyleSheet("QMainWindow { background-image: url(:/images/metal400x200.png) } ");

你是如何培养你的孩子的?你在布局方法中做什么?代码的重要部分没有显示。我刚刚用构建过程的一部分编辑了这个问题。对了,QMainWindow在那里做什么?@MarcSJ请参考并搜索页面上的“背景图像”。对QMainWindow的引用会导致主窗口图像的背景受样式表的影响。如果您的“FenPrincipale”小部件派生自其他类,请用它派生的类替换QMainWindow。