我正在尝试使用Qt绘制一个基本方块,我的问题是颜色与我预期的不同。我想在红色边框的贪婪背景上的蓝色方块。相反,我得到了你在图片上看到的东西。应该怎么做?
#include <QPixmap>
#include <QPainter>
#include <QRect>
#include <QLabel>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap pix(200,100);
QPainter painter(&pix);
painter.setBackground(QBrush("#1ac500")); //green
painter.setPen(QColor("#bd3758")); //red
painter.setBrush(QBrush("#377dbd"));//blue
QRect rect(10,10,50,50);
painter.drawRect(rect);
painter.end();
ui->label->setPixmap(pix);
}
我也试过了,但效果相同
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QPixmap pix(200,100);
QPainter painter(&pix);
painter.setBackground(QBrush("#1ac500")); //green
painter.setPen(Qt::red); //red
QRect rect(10,10,50,50);
painter.fillRect(rect,Qt::red);
painter.drawRect(rect);
painter.end();
ui->label->setPixmap(pix);
}
我的.ui文件
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="windowModality">
<enum>Qt::NonModal</enum>
</property>
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1920</width>
<height>1080</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>50</x>
<y>50</y>
<width>1820</width>
<height>980</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1920</width>
<height>25</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionGenerate_Map"/>
<addaction name="actionLoad_Map"/>
</widget>
<widget class="QMenu" name="menuEdit">
<property name="title">
<string>Edit</string>
</property>
<addaction name="actionPreferences"/>
</widget>
<widget class="QMenu" name="menuHelp">
<property name="title">
<string>Help</string>
</property>
<addaction name="actionAbout"/>
<addaction name="actionAuthor"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEdit"/>
<addaction name="menuHelp"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionGenerate_Map">
<property name="text">
<string>Generate Map</string>
</property>
</action>
<action name="actionPreferences">
<property name="text">
<string>Preferences</string>
</property>
</action>
<action name="actionAbout">
<property name="text">
<string>About</string>
</property>
</action>
<action name="actionAuthor">
<property name="text">
<string>Author</string>
</property>
</action>
<action name="actionLoad_Map">
<property name="text">
<string>Load Map</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>