子类化QWidget IconEditor实现

现在我们从iconEditor构造函数入手:

iconeditor::iconeditor(QWidget *parent) :
    QWidget(parent)
{
    setAttribute(Qt::WA_StaticContents);
    setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
    curColor = Qt::black;
    zoom = 8;
    image = QImage(16, 16, QImage::Format_ARGB32);
    image.fill(qRgba(0, 0, 0, 0));
}
 

setAttribute(Qt::WA_StaticConents);  

Qt::WA_StaticContents属性。
这个属性告诉Qt,当重新改变窗口部件大小时,这个窗口部件的内容并没有发生变化,而且它的内容仍旧保留从窗口左上角开始的特性。当重新定义窗口部件的大小时,通过使用这个信息,Qt就可以避免对已经显示区域的重新绘制。
       通常情况下,当重新定义一个窗口部件的大小时,Qt会为窗口部件的整个可见区域生成一个绘制事件。但是如果该窗口部件在创建时使用了Qt:: WA_StaticContents属性,那么绘制事件的区域就会被严格限定在之前没有被显示的像素部分上。这也就意味着,如果重新把窗口部件改变为比原来还要小的尺寸,那么就根本不会产生任何绘制事件。


setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);

原型:void setSizePolicy ( QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical )

enum QSizePolicy::Policy

This enum describes the various per-dimension sizing types used when constructing a QSizePolicy.

ConstantValueDescription
QSizePolicy::Fixed0The QWidget::sizeHint() is the only acceptable alternative, so the widget can never grow or shrink (e.g. the vertical direction of a push button).
QSizePolicy::MinimumGrowFlagThe sizeHint() is minimal, and sufficient. The widget can be expanded, but there is no advantage to it being larger (e.g. the horizontal direction of a push button). It cannot be smaller than the size provided by sizeHint().
QSizePolicy::MaximumShrinkFlagThe sizeHint() is a maximum. The widget can be shrunk any amount without detriment if other widgets need the space (e.g. a separator line). It cannot be larger than the size provided by sizeHint().
QSizePolicy::PreferredGrowFlag | ShrinkFlagThe sizeHint() is best, but the widget can be shrunk and still be useful. The widget can be expanded, but there is no advantage to it being larger than sizeHint() (the default QWidget policy).
QSizePolicy::ExpandingGrowFlag | ShrinkFlag | ExpandFlagThe sizeHint() is a sensible size, but the widget can be shrunk and still be useful. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider).
QSizePolicy::MinimumExpandingGrowFlag | ExpandFlagThe sizeHint() is minimal, and sufficient. The widget can make use of extra space, so it should get as much space as possible (e.g. the horizontal direction of a horizontal slider).
QSizePolicy::IgnoredShrinkFlag | GrowFlag | IgnoreFlagThe sizeHint() is ignored. The widget will get as much space as possible.

Qt颜色的设置,下面列出了QT里面的颜色


image = QImage(32, 16, QImage::Format_ARGB32);
好吧、先让我们看看原型:

QImage::QImage ( int width, int height, Format format )

关于Format的定义:
QImage::Format_ARGB325The image is stored using a 32-bit ARGB format (0xAARRGGBB).
设置为32位的ARGB格式。

其他实现部分见下一章。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在基于QWidgetQt应用程序中,可以通过以下步骤来实现MVC(Model-View-Controller)框架: 1. Model(模型):模型是应用程序数据的底层表示。可以使用自定义的C++类来作为模型,该类封装了数据和相关的操作。确保模型类继承自QObject类,并使用信号和槽机制来通知视图和控制器数据的变。 ```cpp // MyModel.h #ifndef MYMODEL_H #define MYMODEL_H #include <QObject> class MyModel : public QObject { Q_OBJECT public: explicit MyModel(QObject *parent = nullptr); // 定义模型的数据操作方法 int getData() const; void increment(); signals: // 定义数据变的信号 void dataChanged(); private: int m_data; }; #endif // MYMODEL_H ``` ```cpp // MyModel.cpp #include "MyModel.h" MyModel::MyModel(QObject *parent) : QObject(parent), m_data(0) { } int MyModel::getData() const { return m_data; } void MyModel::increment() { m_data++; emit dataChanged(); } ``` 2. View(视图):视图是用户界面的可视表示。可以使用QWidget或其子类来创建视图,并在视图中展示模型的数据。视图可以通过连接模型的信号和槽来监听数据的变,并及时更新界面。 ```cpp // MyView.h #ifndef MYVIEW_H #define MYVIEW_H #include <QWidget> class QLabel; class QPushButton; class MyModel; class MyView : public QWidget { Q_OBJECT public: explicit MyView(QWidget *parent = nullptr); void setModel(MyModel *model); private slots: void handleIncrementButtonClicked(); private: QLabel *m_dataLabel; QPushButton *m_incrementButton; MyModel *m_model; }; #endif // MYVIEW_H ``` ```cpp // MyView.cpp #include "MyView.h" #include "MyModel.h" #include <QLabel> #include <QPushButton> MyView::MyView(QWidget *parent) : QWidget(parent), m_model(nullptr) { m_dataLabel = new QLabel(this); m_incrementButton = new QPushButton("Increment", this); connect(m_incrementButton, &QPushButton::clicked, this, &MyView::handleIncrementButtonClicked); } void MyView::setModel(MyModel *model) { m_model = model; if (m_model) { connect(m_model, &MyModel::dataChanged, [=]() { m_dataLabel->setText(QString::number(m_model->getData())); }); } } void MyView::handleIncrementButtonClicked() { if (m_model) { m_model->increment(); } } ``` 3. Controller(控制器):控制器是模型和视图之间的中间层,负责处理用户输入和更新模型数据。可以使用QWidget或其子类作为控制器,并通过连接视图的信号和槽来监听用户交互事件。 ```cpp // MyController.h #ifndef MYCONTROLLER_H #define MYCONTROLLER_H #include <QWidget> class QPushButton; class MyModel; class MyController : public QWidget { Q_OBJECT public: explicit MyController(QWidget *parent = nullptr); void setModel(MyModel *model); private slots: void handleIncrementButtonClicked(); private: QPushButton *m_incrementButton; MyModel *m_model; }; #endif // MYCONTROLLER_H ``` ```cpp // MyController.cpp #include "MyController.h" #include "MyModel.h" #include <QPushButton> MyController::MyController(QWidget *parent) : QWidget(parent), m_model(nullptr) { m_incrementButton = new QPushButton("Increment", this); connect(m_incrementButton, &QPushButton::clicked, this, &MyController::handleIncrementButtonClicked); } void MyController::setModel(MyModel *model) { m_model = model; } void MyController::handleIncrementButtonClicked() { if (m_model) { m_model->increment(); } } ``` 4. 主程序:在主程序中,实例模型、视图和控制器,并将它们连接起来。 ```cpp #include <QApplication> #include "MyModel.h" #include "MyView.h" #include "MyController.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MyModel model; MyView view; MyController controller; view.setModel(&model); controller.setModel(&model); view.show(); controller.show(); return a.exec(); } ``` 通过以上步骤,我们实现了一个基于QWidget的MVC框架。模型负责存储数据,视图负责展示数据,并通过控制器处理用户交互。这种分离和组合的方式可以帮助我们更好地管理UI的逻辑和数据,提高代码的可维护性和可扩展性。 请注意,这只是一个简单的示例,实际的MVC框架可能更加复杂和灵活,需要根据具体应用的需求进行设计和实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值