Qt获得标签以保持一定的折扣

时间:2016-02-02 04:35:45

标签: c++ css wpf qt

我正在制作一个数学应用程序,孩子们将用它来学习基本的数学技能。到目前为止,我已经重新调整了标题标签,并且与顶部保持一定距离,但由于Qt的起源位于左上角,我无法排列底部的开始按钮。非常感谢任何建议的帮助,谢谢!

以下是起始标签离底部的距离:

enter image description here

当我将尺寸调整得过宽时,会出现以下情况:

enter image description here

Chalkboard.cpp:

#include "chalkboard.h"
#include "ui_chalkboard.h"

Chalkboard::Chalkboard(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Chalkboard)
{
    ui->setupUi(this);
    //setWindowFlags(Qt::Window | Qt::FramelessWindowHint);

    id = QFontDatabase::addApplicationFont(":/fonts/chawp.ttf");
    family = QFontDatabase::applicationFontFamilies(id).at(0);
    chawp = family;
    setFont(chawp);

    scene = new QGraphicsScene(100, 100, 100, 100);
    ui->graphicsView->setScene(scene);
    image = new QImage(":/images/depositphotos_40177799-Seamless-Chalkboard-Texture.jpg");
    brush = new QBrush(*image);
    ui->graphicsView->setBackgroundBrush(*brush);

    titleEffect = new QGraphicsDropShadowEffect();
    titleEffect->setBlurRadius(10);
    titleEffect->setColor(QColor("#e0dbd1"));
    titleEffect->setXOffset(0);
    titleEffect->setYOffset(0);

    startEffect = new QGraphicsDropShadowEffect();
    startEffect->setBlurRadius(10);
    startEffect->setColor(QColor("#e0dbd1"));
    startEffect->setXOffset(0);
    startEffect->setYOffset(0);

    ui->labelTitle->setStyleSheet("color: #e0dbd1;font: url(:/font/chawp.ttf);");
    ui->labelStart->setStyleSheet("color: #e0dbd1;font: url(:/font/chawp.ttf);");
    ui->labelTitle->setGraphicsEffect(titleEffect);
    ui->labelStart->setGraphicsEffect(startEffect);
}

Chalkboard::~Chalkboard()
{
    delete ui;
}

void Chalkboard::resizeEvent(QResizeEvent *event)
{
    QFontMetrics temp(chawp);
    if (windowState() != Qt::WindowFullScreen)
    {
        setMaximumSize(1920, 1080);
    }

    QFont temp1(chawp);
    QFont temp2(chawp);
    temp1.setPixelSize(width()/10);
    temp2.setPixelSize(width()/15);

    ui->graphicsView->move(0, 0);
    ui->graphicsView->resize(width(), height());

    ui->labelTitle->resize(width(), height());
    ui->labelTitle->move(0, 15);
    ui->labelTitle->setFont(temp1);

    //My failed attempt at it:
    if (height()/5 < 75)
    {
        ui->labelStart->resize(width(), height());
        ui->labelStart->move(0, height() - (height() / 5));
        ui->labelStart->setFont(temp2);
    }
}

Main.cpp的:

#include "chalkboard.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Chalkboard w;
    w.show();

    return a.exec();
}

Chalkboard.h:

#ifndef CHALKBOARD_H
#define CHALKBOARD_H

#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QFontDatabase>
#include <QDebug>
#include <QGraphicsDropShadowEffect>

namespace Ui {
class Chalkboard;
}

class Chalkboard : public QMainWindow
{
    Q_OBJECT

public:
    explicit Chalkboard(QWidget *parent = 0);
    ~Chalkboard();

public slots:
    void resizeEvent(QResizeEvent* event);

private:
    QGraphicsDropShadowEffect * titleEffect;
    QGraphicsDropShadowEffect * startEffect;
    QFont chawp;
    QGraphicsScene *scene;
    QString family;
    int id;
    QImage *image;
    QBrush *brush;
    Ui::Chalkboard *ui;
};

#endif // CHALKBOARD_H

Chalkboard.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Chalkboard</class>
 <widget class="QMainWindow" name="Chalkboard">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>644</width>
    <height>468</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>644</width>
    <height>468</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>Chalkboard</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QGraphicsView" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>20</y>
      <width>61</width>
      <height>71</height>
     </rect>
    </property>
   </widget>
   <widget class="QLabel" name="labelStart">
    <property name="geometry">
     <rect>
      <x>120</x>
      <y>290</y>
      <width>311</width>
      <height>131</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>35</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Start</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignHCenter|Qt::AlignTop</set>
    </property>
   </widget>
   <widget class="QLabel" name="labelTitle">
    <property name="geometry">
     <rect>
      <x>100</x>
      <y>50</y>
      <width>391</width>
      <height>151</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <pointsize>50</pointsize>
     </font>
    </property>
    <property name="text">
     <string>Chalkboard</string>
    </property>
    <property name="alignment">
     <set>Qt::AlignHCenter|Qt::AlignTop</set>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>220</y>
      <width>131</width>
      <height>61</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
    <property name="icon">
     <iconset resource="resource.qrc">
      <normaloff>:/images/liberty-technology-arrow-1.png</normaloff>:/images/liberty-technology-arrow-1.png</iconset>
    </property>
   </widget>
   <widget class="QPushButton" name="pushButton_2">
    <property name="geometry">
     <rect>
      <x>444</x>
      <y>200</y>
      <width>111</width>
      <height>61</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
    <property name="icon">
     <iconset resource="resource.qrc">
      <normaloff>:/images/liberty-technology-arrow-2.png</normaloff>:/images/liberty-technology-arrow-2.png</iconset>
    </property>
   </widget>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources>
  <include location="resource.qrc"/>
 </resources>
 <connections/>
</ui>

2 个答案:

答案 0 :(得分:0)

我终于通过简单地将QLabel的Y轴设置为(height()-(ui->pushButtonStart->height()))

来解决这个问题

答案 1 :(得分:0)

另一种解决方案:

  1. 创建一个QFrame窗口小部件,并将标签和按钮放在里面,并附上必要的布局和间隔符。

  2. 在你的resize事件处理程序中,只需为QGraphicsView设置适当的宽度和高度(占据整个父窗口小部件区域)。

  3. 我已将您的.ui文件修改为提示:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Chalkboard</class>
     <widget class="QMainWindow" name="Chalkboard">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>644</width>
        <height>468</height>
       </rect>
      </property>
      <property name="minimumSize">
       <size>
        <width>644</width>
        <height>468</height>
       </size>
      </property>
      <property name="windowTitle">
       <string>Chalkboard</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <widget class="QGraphicsView" name="graphicsView">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>20</y>
          <width>311</width>
          <height>341</height>
         </rect>
        </property>
       </widget>
       <widget class="QFrame" name="frame">
        <property name="geometry">
         <rect>
          <x>210</x>
          <y>10</y>
          <width>349</width>
          <height>401</height>
         </rect>
        </property>
        <property name="frameShape">
         <enum>QFrame::StyledPanel</enum>
        </property>
        <property name="frameShadow">
         <enum>QFrame::Raised</enum>
        </property>
        <layout class="QVBoxLayout" name="verticalLayout">
         <item>
          <widget class="QLabel" name="labelTitle">
           <property name="font">
            <font>
             <pointsize>50</pointsize>
            </font>
           </property>
           <property name="text">
            <string>Chalkboard</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignHCenter|Qt::AlignTop</set>
           </property>
          </widget>
         </item>
         <item>
          <spacer name="verticalSpacer">
           <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>
          <layout class="QHBoxLayout" name="horizontalLayout">
           <item>
            <widget class="QPushButton" name="pushButton">
             <property name="text">
              <string/>
             </property>
             <property name="icon">
              <iconset>
               <normaloff>:/images/liberty-technology-arrow-1.png</normaloff>:/images/liberty-technology-arrow-1.png</iconset>
             </property>
            </widget>
           </item>
           <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="QPushButton" name="pushButton_2">
             <property name="text">
              <string/>
             </property>
             <property name="icon">
              <iconset>
               <normaloff>:/images/liberty-technology-arrow-2.png</normaloff>:/images/liberty-technology-arrow-2.png</iconset>
             </property>
            </widget>
           </item>
          </layout>
         </item>
         <item>
          <spacer name="verticalSpacer_2">
           <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="QLabel" name="labelStart">
           <property name="font">
            <font>
             <pointsize>35</pointsize>
            </font>
           </property>
           <property name="text">
            <string>Start</string>
           </property>
           <property name="alignment">
            <set>Qt::AlignHCenter|Qt::AlignTop</set>
           </property>
          </widget>
         </item>
        </layout>
       </widget>
       <zorder>graphicsView</zorder>
       <zorder>frame</zorder>
       <zorder>labelTitle</zorder>
      </widget>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources>
      <include location="resource.qrc"/>
     </resources>
     <connections/>
    </ui>