• 6555阅读
  • 4回复

帮忙看道简单的程序!!!!!!! [复制链接]

上一主题 下一主题
离线耗生
 
只看楼主 倒序阅读 楼主  发表于: 2009-08-11
代码如下,这是我运行的结果,怎么觉得不对呀。
 

  1. //iconeditor.h
  2. #ifndef ICONEDITOR_H
  3. #define ICONEDITOR_H
  4. #include <QColor>
  5. #include <QImage>
  6. #include <QWidget>
  7. class IconEditor : public QWidget
  8. {
  9. Q_OBJECT
  10. Q_PROPERTY(QColor penColor READ penColor WRITE setPenColor)
  11. Q_PROPERTY(QImage iconImage READ iconImage WRITE setIconImage)
  12. Q_PROPERTY(int zoomFactor READ zoomFactor WRITE setZoomFactor)
  13. public:
  14. IconEditor(QWidget *parent = 0);
  15. void setPenColor(const QColor &newColor);
  16. QColor penColor() const { return curColor; }
  17. void setZoomFactor(int newZoom);
  18. int zoomFactor() const { return zoom; }
  19. void setIconImage(const QImage &newImage);
  20. QImage iconImage() const { return image; }
  21. QSize sizeHint() const;
  22. protected:
  23. void mousePressEvent(QMouseEvent *event);
  24. void mouseMoveEvent(QMouseEvent *event);
  25. void paintEvent(QPaintEvent *event);
  26. private:
  27. void setImagePixel(const QPoint &pos, bool opaque);
  28. QRect pixelRect(int i, int j) const;
  29. QColor curColor;
  30. QImage image;
  31. int zoom;
  32. };
  33. #endif
  34. //iconeditor.cpp
  35. #include <QtGui>
  36. #include "iconeditor.h"
  37. IconEditor::IconEditor(QWidget *parent)
  38. : QWidget(parent)
  39. {
  40. setAttribute(Qt::WA_StaticContents);
  41. setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
  42. curColor = Qt::black;
  43. zoom = 8;
  44. image = QImage(16, 16, QImage::Format_ARGB32);
  45. image.fill(qRgba(0, 0, 0, 0));
  46. }
  47. void IconEditor::setPenColor(const QColor &newColor)
  48. {
  49. curColor = newColor;
  50. }
  51. void IconEditor::setZoomFactor(int newZoom)
  52. {
  53. if (newZoom < 1)
  54. newZoom = 1;
  55. if (newZoom != zoom) {
  56. zoom = newZoom;
  57. update();
  58. updateGeometry();
  59. }
  60. }
  61. void IconEditor::setIconImage(const QImage &newImage)
  62. {
  63. if (newImage != image) {
  64. image = newImage.convertToFormat(QImage::Format_ARGB32);
  65. update();
  66. updateGeometry();
  67. }
  68. }
  69. QSize IconEditor::sizeHint() const
  70. {
  71. QSize size = zoom * image.size();
  72. if (zoom >= 3)
  73. size += QSize(1, 1);
  74. return size;
  75. }
  76. void IconEditor::mousePressEvent(QMouseEvent *event)
  77. {
  78. if (event->button() == Qt::LeftButton) {
  79. setImagePixel(event->pos(), true);
  80. } else if (event->button() == Qt::RightButton) {
  81. setImagePixel(event->pos(), false);
  82. }
  83. }
  84. void IconEditor::mouseMoveEvent(QMouseEvent *event)
  85. {
  86. if (event->buttons() & Qt::LeftButton) {
  87. setImagePixel(event->pos(), true);
  88. } else if (event->buttons() & Qt::RightButton) {
  89. setImagePixel(event->pos(), false);
  90. }
  91. }
  92. void IconEditor::paintEvent(QPaintEvent *event)
  93. {
  94. QPainter painter(this);
  95. if (zoom >= 3) {
  96. painter.setPen(palette().foreground().color());
  97. for (int i = 0; i <= image.width(); ++i)
  98. painter.drawLine(zoom * i, 0,
  99. zoom * i, zoom * image.height());
  100. for (int j = 0; j <= image.height(); ++j)
  101. painter.drawLine(0, zoom * j,
  102. zoom * image.width(), zoom * j);
  103. }
  104. for (int i = 0; i < image.width(); ++i) {
  105. for (int j = 0; j < image.height(); ++j) {
  106. QRect rect = pixelRect(i, j);
  107. if (!event->region().intersect(rect).isEmpty()) {
  108. QColor color = QColor::fromRgba(image.pixel(i, j));
  109. if (color.alpha() < 255)
  110. painter.fillRect(rect, Qt::white);
  111. painter.fillRect(rect, color);
  112. }
  113. }
  114. }
  115. }
  116. void IconEditor::setImagePixel(const QPoint &pos, bool opaque)
  117. {
  118. int i = pos.x() / zoom;
  119. int j = pos.y() / zoom;
  120. if (image.rect().contains(i, j)) {
  121. if (opaque) {
  122. image.setPixel(i, j, penColor().rgba());
  123. } else {
  124. image.setPixel(i, j, qRgba(0, 0, 0, 0));
  125. }
  126. update(pixelRect(i, j));
  127. }
  128. }
  129. QRect IconEditor::pixelRect(int i, int j) const
  130. {
  131. if (zoom >= 3) {
  132. return QRect(zoom * i + 1, zoom * j + 1, zoom - 1, zoom - 1);
  133. } else {
  134. return QRect(zoom * i, zoom * j, zoom, zoom);
  135. }
  136. }
  137. //main.cpp
  138. #include <QApplication>
  139. #include "iconeditor.h"
  140. int main(int argc, char *argv[])
  141. {
  142. QApplication app(argc, argv);
  143. IconEditor iconEditor;
  144. iconEditor.setWindowTitle(QObject::tr("Icon Editor"));
  145. iconEditor.setIconImage(QImage(":/images/mouse.png"));
  146. iconEditor.show();
  147. return app.exec();
  148. }


初来咋到……
离线耗生
只看该作者 1楼 发表于: 2009-08-12
学QT,确实像他们说的,遇到什么问题,半天得不到解决方法。学MFC就不同了。
彷徨……
初来咋到……
离线dbzhang800

只看该作者 2楼 发表于: 2009-08-12
引用第1楼耗生于2009-08-12 20:13发表的  :
学QT,确实像他们说的,遇到什么问题,半天得不到解决方法。学MFC就不同了。
彷徨……

建议看看 提问的智慧 这篇文章
离线yu2212
只看该作者 3楼 发表于: 2009-08-13
真的,你觉得哪里不对,你自己应该把问题描述清楚。。

而不是要别人帮你找错嘛。。
离线bingogo
只看该作者 4楼 发表于: 2009-08-13
什么不对?
怎么不对?

别人都不知道你在问什么?
怎么帮你解决问题?

一般来说,把问题描述清楚了,你自己就可以找到答案


我猜(对,没错,是 **我猜**):
你的问题是,在QWidget里,指定的图片没有显示,是吗?

在你的代码里,我似乎没有看到你想让QImage显示在哪里……
快速回复
限100 字节
 
上一个 下一个