无法绘制或更改像素' QImage上的颜色

时间:2017-06-03 13:54:30

标签: c++ qt

我是QT的新手,现在遇到功能QImage::setPixelQPainter::drawPoint的问题

我有主窗口和窗口小部件,称为绘图区域。绘图区域使用带有标签的布局,其中包含转换为QPixmap的QImage。不幸的是,我用来绘制点的函数没有结果。

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{ 
ui->setupUi(this);

area = new DrawingArea(this);
area->setGeometry(0,0,this->width(),this->height()/2);
area->show();

button = new QPushButton("Draw", this);
int bwidth = 100, bheight = 50;
button->setGeometry(200, 300, bwidth, bheight);
connect(button, SIGNAL(clicked(bool)), this, SLOT(getPoint()));
}

MainWindow::~MainWindow()
{
delete ui;
delete button;
delete area;
}

void MainWindow::getPoint(){
area->clearPoints();
area->makePoint(ui->textEdit->toPlainText().toInt(), ui->textEdit_2-
>toPlainText().toInt());
area->makePoint(ui->textEdit_3->toPlainText().toInt(), ui->textEdit_4-
>toPlainText().toInt());
area->makePoint(ui->textEdit_5->toPlainText().toInt(), ui->textEdit_6-
>toPlainText().toInt());
area->showPoints();
}

DrawinArea.cpp

#include "drawingarea.h"
#include <QHBoxLayout>
#include <QPen>
#include <QPainter>

DrawingArea::DrawingArea(QWidget *parent) : QWidget(parent)
{
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);

canvas = new QImage(600, 500, QImage::Format_RGB32);
QRgb val = qRgb(189,149,39);
canvas->fill(Qt::gray);
canvas->setPixel(4,4,val);
canvas->setPixel(5,4,val);

imgDisplayer = new QLabel;
imgDisplayer->setPixmap(QPixmap::fromImage(*canvas));


displayer = new QLabel;

auto *layout = new QHBoxLayout(this);
layout->addWidget(imgDisplayer);
layout->addWidget(displayer);
}

void DrawingArea::paintEvent(QPaintEvent * /* event */){
//
}
void DrawingArea::clearPoints(){
points.clear();
}

void DrawingArea::makePoint(int x, int y){
Point *temp = new Point(x,y);
points.push_back(*temp);
free(temp);
}

void DrawingArea::showPoints(){

displayer->clear();
QPainter painter(canvas);
QPen pen;
pen.setWidth(1);
pen.setColor(Qt::red);
painter.setBrush(Qt::NoBrush);
painter.setPen(pen);
painter.end();
QString text;
for(size_t i = 0; i < points.size();i++){
    text+= ("P" + QString::number(i) + " (X: " + 
QString::number(points[i].x) +
            "; Y: " + QString::number(points[i].y) + ");\n");
    painter.drawPoint(points[i].x, points[i].y);
}
imgDisplayer->setPixmap(QPixmap::fromImage(*canvas));
displayer->setText(text);
displayer->setGeometry(300, 80, 100,100);




displayer->show();
imgDisplayer->show();

}

DrawingArea::~DrawingArea(){
delete canvas;
delete displayer;
delete imgDisplayer;
}

1 个答案:

答案 0 :(得分:0)

这是一个最小的例子。希望有所帮助:)

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QImage>
#include <QPainter>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->drawButton, &QPushButton::clicked, [=](){ // Lambda to keep example small. You may also just have a own method
        auto x = ui->xLineEdit->text().toInt();
        auto y = ui->yLineEdit->text().toInt();

        const QPixmap *pixmap = ui->imageLabel->pixmap();
        QImage image(QSize(600, 400), QImage::Format_RGB32);
        if (pixmap) {
            image = pixmap->toImage();
        } else {
            image.fill(Qt::gray);
        }

        QPainter painter(&image);
        auto pen = painter.pen();
        pen.setColor(Qt::red);
        pen.setWidth(5);
        painter.setPen(pen);
        painter.drawPoint(x, y);
        ui->imageLabel->setPixmap(QPixmap::fromImage(image));
        ui->textLabel->setText(ui->textLabel->text().append("\nP(%1,%2)").arg(QString::number(x), QString::number(y)));
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

mainwindow.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>521</width>
    <height>450</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <widget class="QLabel" name="imageLabel">
        <property name="sizePolicy">
         <sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="text">
         <string/>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QLabel" name="textLabel">
        <property name="text">
         <string>Points:</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <layout class="QGridLayout" name="gridLayout">
      <item row="1" column="0">
       <widget class="QLabel" name="label_2">
        <property name="text">
         <string>y:</string>
        </property>
       </widget>
      </item>
      <item row="1" column="1">
       <widget class="QLineEdit" name="yLineEdit"/>
      </item>
      <item row="0" column="1">
       <widget class="QLineEdit" name="xLineEdit"/>
      </item>
      <item row="0" column="0">
       <widget class="QLabel" name="label">
        <property name="text">
         <string>x:</string>
        </property>
       </widget>
      </item>
      <item row="0" column="2">
       <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>
     </layout>
    </item>
    <item>
     <widget class="QPushButton" name="drawButton">
      <property name="text">
       <string>Draw</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>521</width>
     <height>22</height>
    </rect>
   </property>
  </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"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

祝你好运