C++ Qt获取标签以保持底部的特定百分比
我正在制作一个数学应用程序,供孩子们学习基本的数学技能。到目前为止,我已经重新调整了标题标签的大小,并与顶部保持一定的距离,但是由于Qt的原点位于左上角,我无法对齐底部的“开始”按钮。非常感谢您的建议,谢谢 以下是开始标签离底部的距离: 以下是当我重新调整尺寸时发生的情况: Chalkboard.cpp:C++ Qt获取标签以保持底部的特定百分比,c++,css,wpf,qt,C++,Css,Wpf,Qt,我正在制作一个数学应用程序,供孩子们学习基本的数学技能。到目前为止,我已经重新调整了标题标签的大小,并与顶部保持一定的距离,但是由于Qt的原点位于左上角,我无法对齐底部的“开始”按钮。非常感谢您的建议,谢谢 以下是开始标签离底部的距离: 以下是当我重新调整尺寸时发生的情况: Chalkboard.cpp: #include "chalkboard.h" #include "ui_chalkboard.h" Chalkboard::Chalkboard(QWidget *parent) :
#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);
}
}
#包括“chalkboard.h”
#包括“ui_chalkboard.h”
黑板::黑板(QWidget*父项):
QMainWindow(父级),
用户界面(新用户界面::黑板)
{
用户界面->设置用户界面(此);
//setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
id=QFontDatabase::addApplicationFont(“:/fonts/chawp.ttf”);
family=QFontDatabase::applicationFontFamilies(id).at(0);
chawp=家庭;
setFont(chawp);
场景=新的Qgraphicscene(100100100100);
ui->graphicsView->setScene(场景);
image=new QImage(:/images/depositphotos_40177799-Seamless-Chalkboard-Texture.jpg”);
画笔=新的QBrush(*图像);
ui->graphicsView->setBackgroundBrush(*画笔);
titleEffect=新的QGraphicsDropShadowEffect();
titleEffect->setBlurRadius(10);
titleEffect->setColor(QColor(#e0dbd1”);
titleEffect->setXOffset(0);
titleEffect->setYOffset(0);
startefect=新的QGraphicsDropShadowEffect();
startefect->setBlurRadius(10);
startefect->setColor(QColor(#e0dbd1”);
startefect->setXOffset(0);
startefect->setYOffset(0);
ui->labelTitle->setStyleSheet(“颜色:#e0dbd1;字体:url(:/font/chawp.ttf);”;
ui->labelStart->setStyleSheet(“颜色:#e0dbd1;字体:url(:/font/chawp.ttf);”;
ui->labelTitle->setGraphicsEffect(标题效果);
ui->labelStart->setGraphicsEffect(开始生效);
}
黑板::~黑板()
{
删除用户界面;
}
无效黑板::resizeEvent(QResizeEvent*事件)
{
QFontMetrics temp(chawp);
if(windowState()!=Qt::WindowFullScreen)
{
setMaximumSize(19201080);
}
QFont temp1(chawp);
QFont-temp2(chawp);
temp1.setPixelSize(宽度()/10);
temp2.setPixelSize(宽度()/15);
用户界面->图形视图->移动(0,0);
用户界面->图形视图->调整大小(宽度(),高度());
用户界面->标签->调整大小(宽度(),高度());
用户界面->标签->移动(0,15);
ui->labelTitle->setFont(temp1);
//我失败的尝试:
如果(高度()/5<75)
{
ui->labelStart->调整大小(宽度(),高度());
用户界面->标签开始->移动(0,高度()-(高度()/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”
#包括
int main(int argc,char*argv[])
{
质量保证申请a(argc、argv);
黑板w;
w、 show();
返回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
\ifndef黑板
#定义黑板
#包括
#包括
#包括
#包括
#包括
#包括
名称空间用户界面{
类黑板;
}
班级黑板:公共QMainWindow
{
Q_对象
公众:
显式黑板(QWidget*parent=0);
~黑板();
公众时段:
void resizeEvent(QResizeEvent*事件);
私人:
QGraphicsDropShadowEffect*标题效果;
QGraphicsDropShadowEffect*startEffect;
QFont chawp;
qgraphicscene*场景;
QString家族;
int-id;
QImage*图像;
QBrush*画笔;
Ui::黑板*Ui;
};
#endif//黑板
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>
黑板
0
0
644
468
644
468
黑板
20
20
61
71
120
290
311
131
35
开始
Qt::AlignHCenter | Qt::AlignTop
100
50
391
151
50
黑板
Qt::AlignHCenter | Qt::AlignTop
40
220
131
61
:/images/liberty-technology-arrow-1.png:/images/liberty-technology-arrow-1.png
444
200
111
61
:/images/liberty-technology-arrow-2.png:/images/liberty-technology-arrow-2.png
我最终解决了这个问题,只需将QLabel的Y轴设置为(高度()-(ui->pushButtonStart->height())
另一个解决方案:
QFrame
小部件,将标签和按钮以及必要的布局和间隔放在里面QGraphicsView
一样设置此框架的适当宽度和高度(以占据整个父窗口小部件区域)<?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>
黑板
0
0
644
468
644
468
黑板
20
20
311
341
210
10
349
401
QFrame::StyledPanel
QFrame::提出
50
黑板
Qt::AlignHCenter | Qt::AlignTop
Qt::垂直
20
40
:/images/liberty-technology-arrow-1.png:/images/liberty-technology-arrow-1.png
Qt::水平
40
20
:/images/liberty-technology-arrow-2.png:/images/liberty-technology-arr