热门
Display::Display(QWidget *parent) : QWidget{parent} { mWidth = 16; mHeight = 9; mVideo = new QWidget(this); //使用setAutoFillBackground函数可以启用或禁用自动填充背景的功能 mVideo->setAutoFillBackground(true); //Qt::WA_StyledBackground是一个用于启用QWidget的样式背景的属性 mVideo->setAttribute(Qt::WA_StyledBackground); mVideo->setStyleSheet("background-color: black;"); this->setAutoFillBackground(true); this->setAttribute(Qt::WA_StyledBackground); this->setStyleSheet("background-color: black;"); } Display::~Display() { delete mVideo; }
void Display::resizeEvent(QResizeEvent* event) { setWidgetSize(); QWidget::resizeEvent(event); } void Display::setWidgetSize() { int widgetWidth = this->width(); int widgetHeight = this->height(); /* * 宽比 宽度 * —— = —— * 高比 高度 * * 根据宽度得到高度 * * 如果得到高度大于实际高度 * * 则: * * 根据高度得到宽度 * */ int tempWidth = widgetWidth; int tempHeight = tempWidth * this->mHeight / this->mWidth; if (tempHeight > widgetHeight) { tempHeight = widgetHeight; tempWidth = tempHeight * this->mWidth / this->mHeight; } //如果计算出来在波动范围内则不动 if (tempWidth >= widgetWidth - 1 && tempWidth <= widgetWidth + 1 && tempHeight >= widgetHeight - 1 && tempHeight <= widgetHeight + 1) return; mVideo->setGeometry((widgetWidth - tempWidth) / 2, (widgetHeight - tempHeight) / 2, tempWidth, tempHeight); } void Display::setNewSize(int width, int height) { mWidth = width; mHeight = height; setWidgetSize(); } QWidget* Display::getVideo() { return mVideo; }