c++ - Qt 获取标签以保持底部的一定百分比

标签 c++ css wpf qt

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

这里是开始标签应该离底部多远:

enter image description here

以下是当我将 widthwize 调整得太远时发生的情况:

enter image description here

黑板.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);
    }
}

主要.cpp:

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

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

    return a.exec();
}

黑板.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

黑板.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>

最佳答案

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

解决了这个问题

关于c++ - Qt 获取标签以保持底部的一定百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35145131/

相关文章:

c++ - 如何避免多个 deleter lambda?

c++ - 如何在 C++ 中处理 "exit current function, if error on current statement"

javascript - 如何比较javascript中两个元素的位置

c# - 如何在 OnRender 中重绘或刷新?

c# - DependencyProperty 忽略了一些 PropertyChanged 调用

c# - ObservableCollection CollectionChanged 更新数据网格

c++ - 公共(public) const 变量或带有 get 函数的私有(private)变量,哪个更可取?

c++ - 绘制opencv?

css - 哪个 CSS hack 可以通过 w3c 验证?

css - Mobile Safari 和 Bootstrap 4 列内容高度 100% 未兑现