Михаиллл
Михаиллл30. April 2019 06:54

Как в 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);
Рекомендуємо хостинг TIMEWEB
Рекомендуємо хостинг TIMEWEB
Stabiles Hosting des sozialen Netzwerks EVILEG. Wir empfehlen VDS-Hosting für Django-Projekte.

Magst du es? In sozialen Netzwerken teilen!

4
Михаиллл
  • 30. April 2019 07:30
  • (bearbeitet)

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

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
    • 30. April 2019 07:37

    Добрый день!

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

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

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

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

      Михаиллл
      • 30. April 2019 07:47
      • (bearbeitet)

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

        Михаиллл
        • 30. April 2019 07:57
        • Die Antwort wurde als Lösung markiert.

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

          Kommentare

          Nur autorisierte Benutzer können Kommentare posten.
          Bitte Anmelden oder Registrieren
          Letzte Kommentare
          A
          ALO1ZE19. Oktober 2024 08:19
          Fb3-Dateileser auf Qt Creator Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html
          ИМ
          Игорь Максимов5. Oktober 2024 07:51
          Django – Lektion 064. So schreiben Sie eine Python-Markdown-Erweiterung Приветствую Евгений! У меня вопрос. Можно ли вставлять свои классы в разметку редактора markdown? Допустим имея стандартную разметку: <ul> <li></li> <li></l…
          d
          dblas55. Juli 2024 11:02
          QML - Lektion 016. SQLite-Datenbank und das Arbeiten damit in QML Qt Здравствуйте, возникает такая проблема (я новичок): ApplicationWindow неизвестный элемент. (М300) для TextField и Button аналогично. Могу предположить, что из-за более новой верси…
          k
          kmssr8. Februar 2024 18:43
          Qt Linux - Lektion 001. Autorun Qt-Anwendung unter Linux как сделать автозапуск для флэтпака, который не даёт создавать файлы в ~/.config - вот это вопрос ))
          Qt WinAPI - Lektion 007. Arbeiten mit ICMP-Ping in Qt Без строки #include <QRegularExpressionValidator> в заголовочном файле не работает валидатор.
          Jetzt im Forum diskutieren
          J
          JacobFib17. Oktober 2024 03:27
          добавить qlineseries в функции Пользователь может получить любые разъяснения по интересующим вопросам, касающимся обработки его персональных данных, обратившись к Оператору с помощью электронной почты https://topdecorpro.ru…
          JW
          Jhon Wick1. Oktober 2024 15:52
          Indian Food Restaurant In Columbus OH| Layla’s Kitchen Indian Restaurant If you're looking for a truly authentic https://www.laylaskitchenrestaurantohio.com/ , Layla’s Kitchen Indian Restaurant is your go-to destination. Located at 6152 Cleveland Ave, Colu…
          КГ
          Кирилл Гусарев27. September 2024 09:09
          Не запускается программа на Qt: точка входа в процедуру не найдена в библиотеке DLL Написал программу на C++ Qt в Qt Creator, сбилдил Release с помощью MinGW 64-bit, бинарнику напихал dll-ки с помощью windeployqt.exe. При попытке запуска моей сбилженной программы выдаёт три оши…
          F
          Fynjy22. Juli 2024 04:15
          при создании qml проекта Kits есть но недоступны для выбора Поставил Qt Creator 11.0.2. Qt 6.4.3 При создании проекта Qml не могу выбрать Kits, они все недоступны, хотя настроены и при создании обычного Qt Widget приложения их можно выбрать. В чем может …

          Folgen Sie uns in sozialen Netzwerken