Михаиллл
МихаилллApril 30, 2019, 6:54 a.m.

Как в Qt задать картинку в виде фона интерфейса

Добрый день.
Как задать картинку в виде фона интерфейса?
Пробовал так, но не заработало:

QPixmap background("D:\\QTProject\\vedaEMailClient\\vedaEMailClient\\Images\\splash-background.png");
    QImage background2("D:\\QTProject\\vedaEMailClient\\vedaEMailClient\\Images\\splash-background.png");
    //QPixmap background(":/Images/Images/splash-background.png");
    background.scaled(this->size(), Qt::IgnoreAspectRatio);
    QPalette palette;
    palette.setBrush(QPalette::Background, background2);
    this->setPalette(palette);
We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

4
Михаиллл
  • April 30, 2019, 7:30 a.m.
  • (edited)

Вот так работает, но остается белая рамочка вокруг окна и не окрашенный прямоугольник сверху.
Скажите пожалуйста, в чем может быть дело и как это исправить?

QPalette Pal(palette());
    QImage background2(":/Images/Images/splash-background.png");
    Pal.setBrush(QPalette::Background, background2);
    this->setAutoFillBackground(true);
    this->setPalette(Pal);

вот код страницы

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>begin</class>
 <widget class="QWidget" name="begin">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>681</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>800</width>
    <height>600</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="2" column="0">
    <layout class="QHBoxLayout" name="horizontalLayout" stretch="1,1,1">
     <item>
      <spacer name="horizontalSpacer">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QWidget" name="widget" native="true">
       <layout class="QGridLayout" name="gridLayout_2">
        <item row="0" column="0">
         <layout class="QVBoxLayout" name="verticalLayout_5" stretch="1,1">
          <item>
           <layout class="QVBoxLayout" name="verticalLayout_3">
            <item>
             <spacer name="verticalSpacer_3">
              <property name="orientation">
               <enum>Qt::Vertical</enum>
              </property>
              <property name="sizeHint" stdset="0">
               <size>
                <width>20</width>
                <height>40</height>
               </size>
              </property>
             </spacer>
            </item>
            <item>
             <widget class="QLineEdit" name="login_lineEdit">
              <property name="minimumSize">
               <size>
                <width>0</width>
                <height>40</height>
               </size>
              </property>
              <property name="placeholderText">
               <string>login</string>
              </property>
             </widget>
            </item>
            <item>
             <spacer name="verticalSpacer_4">
              <property name="orientation">
               <enum>Qt::Vertical</enum>
              </property>
              <property name="sizeHint" stdset="0">
               <size>
                <width>20</width>
                <height>40</height>
               </size>
              </property>
             </spacer>
            </item>
            <item>
             <widget class="QLineEdit" name="password_lineEdit">
              <property name="minimumSize">
               <size>
                <width>0</width>
                <height>40</height>
               </size>
              </property>
              <property name="placeholderText">
               <string>password</string>
              </property>
             </widget>
            </item>
            <item>
             <spacer name="verticalSpacer_5">
              <property name="orientation">
               <enum>Qt::Vertical</enum>
              </property>
              <property name="sizeHint" stdset="0">
               <size>
                <width>20</width>
                <height>40</height>
               </size>
              </property>
             </spacer>
            </item>
            <item>
             <widget class="QLineEdit" name="secret_lineEdit">
              <property name="minimumSize">
               <size>
                <width>0</width>
                <height>40</height>
               </size>
              </property>
              <property name="placeholderText">
               <string>secret phrase</string>
              </property>
             </widget>
            </item>
            <item>
             <spacer name="verticalSpacer_6">
              <property name="orientation">
               <enum>Qt::Vertical</enum>
              </property>
              <property name="sizeHint" stdset="0">
               <size>
                <width>20</width>
                <height>40</height>
               </size>
              </property>
             </spacer>
            </item>
           </layout>
          </item>
          <item>
           <layout class="QVBoxLayout" name="verticalLayout_4">
            <item>
             <spacer name="verticalSpacer_7">
              <property name="orientation">
               <enum>Qt::Vertical</enum>
              </property>
              <property name="sizeHint" stdset="0">
               <size>
                <width>231</width>
                <height>13</height>
               </size>
              </property>
             </spacer>
            </item>
            <item>
             <layout class="QVBoxLayout" name="verticalLayout_2">
              <item>
               <layout class="QHBoxLayout" name="horizontalLayout_3">
                <item>
                 <spacer name="horizontalSpacer_3">
                  <property name="orientation">
                   <enum>Qt::Horizontal</enum>
                  </property>
                  <property name="sizeHint" stdset="0">
                   <size>
                    <width>40</width>
                    <height>20</height>
                   </size>
                  </property>
                 </spacer>
                </item>
                <item>
                 <widget class="QPushButton" name="signIn_Button">
                  <property name="minimumSize">
                   <size>
                    <width>200</width>
                    <height>70</height>
                   </size>
                  </property>
                  <property name="maximumSize">
                   <size>
                    <width>200</width>
                    <height>16777215</height>
                   </size>
                  </property>
                  <property name="text">
                   <string>Sign in</string>
                  </property>
                 </widget>
                </item>
                <item>
                 <spacer name="horizontalSpacer_4">
                  <property name="orientation">
                   <enum>Qt::Horizontal</enum>
                  </property>
                  <property name="sizeHint" stdset="0">
                   <size>
                    <width>40</width>
                    <height>20</height>
                   </size>
                  </property>
                 </spacer>
                </item>
               </layout>
              </item>
              <item>
               <spacer name="verticalSpacer_8">
                <property name="orientation">
                 <enum>Qt::Vertical</enum>
                </property>
                <property name="sizeHint" stdset="0">
                 <size>
                  <width>231</width>
                  <height>13</height>
                 </size>
                </property>
               </spacer>
              </item>
              <item>
               <layout class="QHBoxLayout" name="horizontalLayout_2">
                <item>
                 <spacer name="horizontalSpacer_5">
                  <property name="orientation">
                   <enum>Qt::Horizontal</enum>
                  </property>
                  <property name="sizeHint" stdset="0">
                   <size>
                    <width>40</width>
                    <height>20</height>
                   </size>
                  </property>
                 </spacer>
                </item>
                <item>
                 <widget class="QPushButton" name="register_Button">
                  <property name="minimumSize">
                   <size>
                    <width>200</width>
                    <height>70</height>
                   </size>
                  </property>
                  <property name="maximumSize">
                   <size>
                    <width>200</width>
                    <height>16777215</height>
                   </size>
                  </property>
                  <property name="text">
                   <string>Register</string>
                  </property>
                 </widget>
                </item>
                <item>
                 <spacer name="horizontalSpacer_6">
                  <property name="orientation">
                   <enum>Qt::Horizontal</enum>
                  </property>
                  <property name="sizeHint" stdset="0">
                   <size>
                    <width>40</width>
                    <height>20</height>
                   </size>
                  </property>
                 </spacer>
                </item>
               </layout>
              </item>
             </layout>
            </item>
            <item>
             <spacer name="verticalSpacer_9">
              <property name="orientation">
               <enum>Qt::Vertical</enum>
              </property>
              <property name="sizeHint" stdset="0">
               <size>
                <width>231</width>
                <height>13</height>
               </size>
              </property>
             </spacer>
            </item>
           </layout>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer_2">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
    </layout>
   </item>
   <item row="1" column="0">
    <layout class="QHBoxLayout" name="horizontalLayout_4">
     <item>
      <spacer name="horizontalSpacer_7">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
     <item>
      <widget class="QLabel" name="labe1PageBegin">
       <property name="text">
        <string>TextLabel</string>
       </property>
      </widget>
     </item>
     <item>
      <spacer name="horizontalSpacer_8">
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="sizeHint" stdset="0">
        <size>
         <width>40</width>
         <height>20</height>
        </size>
       </property>
      </spacer>
     </item>
    </layout>
   </item>
   <item row="3" column="0">
    <spacer name="verticalSpacer_2">
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
     <property name="sizeHint" stdset="0">
      <size>
       <width>20</width>
       <height>30</height>
      </size>
     </property>
    </spacer>
   </item>
   <item row="0" column="0">
    <spacer name="verticalSpacer">
     <property name="orientation">
      <enum>Qt::Vertical</enum>
     </property>
     <property name="sizeHint" stdset="0">
      <size>
       <width>20</width>
       <height>30</height>
      </size>
     </property>
    </spacer>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

    Evgenii Legotckoi
    • April 30, 2019, 7:37 a.m.

    Добрый день!

    Там скорее всего в дизайнере для Layout нужно margins выставить.

    Либо через setStyleSheet установить нулевые padding

    setStyleSheet("QWidget { padding: 3px }");
    

    Но осторожнее, может примениться на все child виджеты.

      Михаиллл
      • April 30, 2019, 7:47 a.m.
      • (edited)

      Написал так и картинка перестала отображаться.
      В дизайнере пробовал менять margins, но ничего не поменялось.

        Михаиллл
        • April 30, 2019, 7:57 a.m.
        • The answer was marked as a solution.

        Попробовал сделать в новом тестовом окне, там все прекрасно.
        Похоже касяк где то в .ui

          Comments

          Only authorized users can post comments.
          Please, Log in or Sign up
          AD

          C ++ - Test 004. Pointers, Arrays and Loops

          • Result:50points,
          • Rating points-4
          m

          C ++ - Test 004. Pointers, Arrays and Loops

          • Result:80points,
          • Rating points4
          m

          C ++ - Test 004. Pointers, Arrays and Loops

          • Result:20points,
          • Rating points-10
          Last comments
          i
          innorwallNov. 15, 2024, 6:30 a.m.
          Qt/C++ - Lesson 065. The correspondence of HTTP errors to server response errors in QNetworkAccessManager s Midsummer Classic in Kansas City priligy online pharmacy
          i
          innorwallNov. 15, 2024, 5:44 a.m.
          Qt/C++ - Lesson 039. How to paint stroke in QSqlTableModel by value in the column? Many OPKs would advise users to start using the test strips around day 9 of your cycle, considering day 1 to be the first day of full menstrual flow buy priligy australia
          i
          innorwallNov. 15, 2024, 2:27 a.m.
          Release of C++/Qt and QML application deployment utility CQtDeployer v1.4.0 (Binary Box) optionally substituted alkoxy, optionally substituted alkenyloxy, optionally substituted alkynyloxy, optionally substituted aryloxy, OCH, OC H, OC H, OC H, OC H, OC H, OC H, O C CH, OCH CH OH, O…
          i
          innorwallNov. 14, 2024, 9:26 p.m.
          Qt/C++ - Lesson 031. QCustomPlot – The build of charts with time buy generic priligy We can just chat, and we will not lose too much time anyway
          i
          innorwallNov. 14, 2024, 7:03 p.m.
          Qt/C++ - Lesson 060. Configuring the appearance of the application in runtime I didnt have an issue work colors priligy dapoxetine 60mg revia cost uk August 3, 2022 Reply
          Now discuss on the forum
          t
          tonypeachey1Nov. 15, 2024, 6:04 a.m.
          google domain [url=https://google.com/]domain[/url] domain [http://www.example.com link title]
          i
          innorwallNov. 15, 2024, 5:50 a.m.
          добавить qlineseries в функции priligy for sale Gently flush using an ear syringe
          i
          innorwallNov. 11, 2024, 10:55 a.m.
          Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
          9
          9AnonimOct. 25, 2024, 9:10 a.m.
          Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

          Follow us in social networks