QScrollArea Custom Widget not Repainting Properly - c++

I have a QScrollArea that contains my custom widget. When I change the data of the custom widget(which changes sizeHint() and the widget takes up more space) the end of each line and the last few lines are cut off. I've created a little program to replicate the issue.
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QString>
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
void changeData(const QString &, const unsigned &, const unsigned &);
QSize sizeHint() const override;
protected:
void paintEvent(QPaintEvent *event) override;
private:
QString lines;
unsigned lineSize = 0, rowSize = 0;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
#include <QtWidgets>
Widget::Widget(QWidget *parent) : QWidget(parent)
{
changeData("TESTING\nFUNCTIO\n1234567\nWORDS77\n", 7, 4);
update();
}
void Widget::changeData(const QString &l, const unsigned &lineSz, const unsigned &rowSz)
{
lines = l;
lineSize = lineSz;
rowSize = rowSz;
}
void Widget::paintEvent(QPaintEvent * /*event*/)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QFont defaultFont = painter.font();
QPen defaultPen = painter.pen();
defaultFont.setLetterSpacing(QFont::AbsoluteSpacing, lineSize / 2 - 3);
// set pen and font
painter.setFont(defaultFont);
painter.setPen(defaultPen);
// starting point
QPoint prntPoint(5, 15);
for (QString::size_type ind = 0, row = 1; ind < lines.size(); ++ind)
{
if (lines[ind] != '\n') // if it's a letter
{
// print the letter and move the point to the right
painter.drawText(prntPoint, QString(lines[ind]));
prntPoint.setX(prntPoint.x() + 20);
}
else if (lines[ind] == '\n') // if the character is a newline
{
prntPoint.setX(5); // move point back to the very left
prntPoint.setY(row * 20 + 15); // move down
++row;
}
}
}
QSize Widget::sizeHint() const
{
return QSize(lineSize * 20, rowSize * 20);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget *w = new Widget;
QScrollArea area;
area.setWidget(w);
area.show();
//change string and other data variables
w->changeData("1234567890\nQWERTYU123\nLETTERS123\nWORDSSS123\n"
"TESTING123\nFUNCTIO123\nLETTERS123\nWORDSSS123\n", 10, 8);
return a.exec();
}
As you can see the data we set in the main function is cut off, only the first 7 characters and first 4 lines are printed, basically taking the first string we set in the constructor and taking a subset of this new string and printing it.
Removing QScrollArea fixes the issue but I need the scrolling capabilities for my application. Any help will be appreciated. Thank you

Related

How can I write a (very) basic custom QAbstractTextDocumentLayout subclass?

I'm trying to create a simple notepad-style Qt application. Since I think I might want a lot of control over exactly how text is rendered, I'm trying to write a custom QAbstractTextDocumentLayout class. The code below compiles and runs, but the text area is blank, no matter how much I type. The debug statements in my draw() function correctly show the entered text, so I know my input is in some form making it all the way to the draw() function, but it's not being rendered.
I just want to tweak this in a minimal way so I can at least see some text being rendered, even if the implementation is incomplete. I also haven't been able to find any documentation whatsoever on writing custom QAbstractTextDocumentLayout, so if anyone had a reference I'd also appreciate that.
main.cpp
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QTextEdit>
#include <QKeyEvent>
#include "./DocumentLayout.cpp"
class MainContent : public QWidget
{
Q_OBJECT
QTextEdit textEdit;
DocumentLayout layout;
public:
MainContent() : textEdit(this), layout(textEdit.document())
{
int innerPadding = 20;
textEdit.document()->setDocumentMargin(innerPadding);
int textColor = 0xD8DEE9;
int backgroundColor = 0x2E3440;
int selectionColor = 0x2E3440;
int selectionBackgroundColor = 0x81A1C1;
QPalette palette = textEdit.palette();
palette.setColor(QPalette::Base, QColor(backgroundColor));
palette.setColor(QPalette::Text, QColor(textColor));
palette.setColor(QPalette::Highlight, QColor(selectionBackgroundColor));
palette.setColor(QPalette::HighlightedText, QColor(selectionColor));
textEdit.setPalette(palette);
textEdit.setFrameStyle(QFrame::NoFrame);
textEdit.document()->setDocumentLayout(&layout);
};
void resizeEvent(QResizeEvent *event)
{
textEdit.setGeometry(0, 0, this->width(), this->height());
}
void keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_W && event->modifiers() == Qt::ControlModifier)
{
qApp->quit();
}
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow window;
window.setWindowTitle("Hello, world!");
window.resize(400, 500);
MainContent content;
window.setCentralWidget(&content);
window.show();
return app.exec();
}
#include "main.moc"
DocumentLayout.cpp
#include <QAbstractTextDocumentLayout>
#include <QPainter>
#include <QTextBlock>
#include <QDebug>
class DocumentLayout : public QAbstractTextDocumentLayout
{
public:
DocumentLayout(QTextDocument *document) : QAbstractTextDocumentLayout(document)
{
}
void draw(QPainter *painter, const PaintContext &context)
{
QTextDocument *document = this->document();
QTextBlock block = document->begin();
QPointF position(20, 20);
painter->setPen(context.palette.color(QPalette::Text));
QTextLayout *layout = block.layout();
layout->draw(painter, position);
position.ry() += layout->boundingRect().height();
qDebug() << "drew" << block.text() << "at" << position;
}
int hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const
{
return 0;
}
int pageCount() const
{
return 1;
}
QSizeF documentSize() const
{
return QSizeF(400, 800);
}
QRectF frameBoundingRect(QTextFrame *frame) const
{
return QRectF(0, 0, 400, 800);
}
QRectF blockBoundingRect(const QTextBlock &block) const
{
return QRectF(0, 0, 400, 800);
}
void documentChanged(int from, int charsRemoved, int charsAdded)
{
}
};

Qt QGraphicsView Scrolling causes items to randomly disappear

I am having issues with a weird bug which occurs after you adjust the size of a QGraphicsItem.
Here is a YouTube video showing the issue: https://youtu.be/gp1lQTkPf54
In my application, a slider is used to adjust the horizontal zoom of all the regions on the QGraphicsScene. I have made sure to call prepareGeometryChange(); when I am changing the geometry of a region and call update(); but that has not helped. It seems to affect regions that are being rendered out of view from the user.
Code:
mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QGraphicsScene *scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
RegionGraphicsItem *rgi = new RegionGraphicsItem(ui->graphicsView->scene());
itemList.append(rgi);
ui->graphicsView->scene()->addItem(rgi);
rgi->setHScaleFactor(ui->horizontalSlider->value());
}
void MainWindow::test() {
QWidget test;
test.show();
}
void MainWindow::on_horizontalSlider_valueChanged(int value)
{
for (int i = 0; i < itemList.size(); i++) {
itemList[i]->setHScaleFactor(value);
}
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "regiongraphicsitem.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QList<RegionGraphicsItem*> itemList;
private slots:
void on_pushButton_clicked();
void on_horizontalSlider_valueChanged(int value);
private:
void test();
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
regiongraphicsitem.cpp
#include "regiongraphicsitem.h"
RegionGraphicsItem::RegionGraphicsItem(QGraphicsScene *_scene) : QGraphicsItem()
{
regionColor = QColor::fromRgb(255,255,255);
setFlags(ItemIsMovable);
waveFormColor = regionColor.darker(80);
outlineColor = QColor("#0f0f0f");
selectedColor = selectedColor.lighter(30);
hScaleFactor = 100;
mainBrush = QBrush(regionColor);
mainPen = QPen(outlineColor, 1);
gridLength = 5;
height = 56;
oldPos = pos();
scene = _scene;
this->prepareGeometryChange();
gridLocation = 5;
setY((0 * 60) + 1);
}
QRectF RegionGraphicsItem::boundingRect() const
{
return QRectF(0, 0, float(gridLength * hScaleFactor), float(height));
}
void RegionGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
QRectF rect = boundingRect();
painter->setBrush(mainBrush);
painter->setPen(mainPen);
painter->drawRoundedRect(rect, 5, 5);
painter->setPen(mainPen);
if (selected) {
painter->setBrush(QBrush(mainPen.color()));
painter->drawRoundedRect(QRect(rect.x(), rect.y(), rect.width(), 20), 5, 5);
painter->drawRect(QRect(rect.x(), rect.y() + 5, rect.width(), 15));
}
QFont font = scene->font();
font.setPixelSize(10);
font.setBold(true);
QFontMetricsF fontMetrics(font);
QString text = "TEST";
int heightFont = fontMetrics.boundingRect(text).height();
if (selected) {
painter->setPen(QPen(mainBrush.color(), 1));
}
painter->drawText(5, heightFont + 3, text);
if (pressed == false ) {
setX((gridLocation - 1) * hScaleFactor);
}
}
void RegionGraphicsItem::setHScaleFactor(int value) {
prepareGeometryChange();
hScaleFactor = value;
update();
}
regiongraphicsitem.h
#ifndef RegionGraphicsItem_H
#define RegionGraphicsItem_H
#include <QGraphicsItem>
#include <QColor>
#include <QBrush>
#include <QPen>
#include <QtGui/QPainter>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
#include <stdint.h>
class RegionGraphicsItem : public QGraphicsItem
{
public:
RegionGraphicsItem(QGraphicsScene *_scene);
float getGridLocation();
void setHScaleFactor(int value);
protected:
QColor outlineColor;
QColor selectedColor;
QColor regionColor;
QColor waveFormColor;
bool selected;
int penWidth;
int rounded;
QBrush mainBrush;
QBrush waveformBrush;
QPen mainPen;
int height;
float gridLength;
bool pressed = false;
QPointF oldPos, oldMousePos;
int oldTrackIndex;
float gridLocation;
QGraphicsScene *scene;
int oldHScaleFactor;
virtual QRectF boundingRect() const override;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private:
int hScaleFactor;
};
#endif // RegionGraphicsItem_H
Steps to reproduce:
Add a RegionGraphicItem to the QGraphicsScene
Increase the slider until the region is off screen
Decrease the slider and the region will not be visible. <-- This is the issue.

Implementations of buttons in Qt / C++

Im looking to implement a sudoku gui in Qt. First things first im trying to generate a grid of QToolButtons, which when pressed will increment their value by one.
Here is my header for window.h
class Window : public QWidget
{
Q_OBJECT
public:
Window(QWidget *parent = nullptr);
private slots:
void digitClicked();
private:
Button *createButton(const QString &text, const char *member);
QString *readFileChar(const QString file_name, QString digit[] );
QGridLayout *previewLayout;
void setSudokuChar(const int digit, const char *member);
enum { NumDigitButtons = 81 };
Button *digitButtons[NumDigitButtons];
QString digit[NumDigitButtons];
protected:
void paintEvent(QPaintEvent *event) override;
void mouseReleaseEvent(QMouseEvent * event) override;
};
And the corresponding source file window.cpp
Window::Window(QWidget *parent) : QWidget(parent) //constrcutor
{
//button creation
QString* current_digit = readFileChar("/home/pi/Documents/ELEC1204/P6/sudoku.txt", digit);
for(int i = 0; i < NumDigitButtons; i++){
digitButtons[i] = createButton(*(current_digit++), SLOT(digitClicked())); //initialize sudoku
}
// set form size
QGridLayout *mainLayout = new QGridLayout;
setMaximumSize(600,600);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
setWindowTitle(tr("button game"));
// place each digit button
for(int i = 1; i < NumDigitButtons; i++){
int row = (81 - i) / 9 ; // y position for button
int col = (i - 1) % 9 ; // x position for button
mainLayout->addWidget(digitButtons[i], row, col);
}
}
void Window::digitClicked()
{
Button *clickedButton = qobject_cast<Button*>(sender()); //clicked button = object button that was pressed
int val = ++(clickedButton->text().toInt()); //incrememt digit value of the button which was pressed
setSudokuChar(val, clickedButton); //updates sudoku with new value
}
QString* Window::readFileChar(const QString file_name, QString digit[]) //reads in file to initialize buttons
{
QFile file(file_name);
int i = 0;
if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream in(&file);
while(!in.atEnd()){
digit[i] = in.read(1); //read each character into an array of QStrings
i++ ;
}
}
else{
QTextStream(stdout) << "could not open file, program terminating" << endl;
exit(1);
}
return digit;
}
Button *Window::createButton(const QString &text, const char *member)
{
Button *button = new Button(text);
connect(button, SIGNAL(clicked()), this, member);
return button;
}
void setSudokuChar(const int digit, Button* clickedButton) //takes in a digit, and assigns its value to the char that was pass
{
}
Please note that the function to update the button's value when pressed 'setSudokuChar' is yet to be implemented, but that pretty irrelevant for now.
Here is the button class header button.h, it's only a small modification of the QToolButton class to aid in resizing.
class Button : public QToolButton
{
Q_OBJECT
public:
explicit Button(const QString &text, QWidget *parent = nullptr);
QSize sizeHint() const override;
};
And finally, here is the button.cpp source file
Button::Button(const QString &text, QWidget *parent) : QToolButton(parent)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
setText(text);
}
QSize Button::sizeHint() const
{
QSize size = QToolButton::sizeHint();
size.rheight() += 20;
size.rwidth() = qMax(size.width(), size.height());
return size;
}
Hopefully thats not too much to digest, I'm still a beginner!
**My problem is ... ** when I run the code, a window is produced but its blank, which means the compilation is fine, but the program is not working as intended. Could anyone by chance spot what is going wrong? All that i do in the main function is create a window as follows :
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window window;
window.show();
return app.exec();
}
Any and all help is thoroughly appreciated.

Subclass of QPlainText does not expand to fill the layout

I do not understand why the CodeEditor example from the Qt website does not appear to work as expected. Every time I run the code it displays it like this, really small and not expanding to take up all the available space. Does anyone have any idea why? I have even tried to set a fixed size, sizepolicy and minimum sizing. Not sure what I am missing here.
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include "codeeditor.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
// controls
auto *widget_main = new QWidget(this);
auto *lay_main = new QVBoxLayout(widget_main);
auto *label = new QLabel("Test");
auto *editor = new CodeEditor();
// layout
lay_main->addWidget(label);
lay_main->addWidget(editor);
lay_main->setContentsMargins(5, 5, 5, 5);
}
MainWindow::~MainWindow()
{
}
codeeditor.h:
#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#include <QPlainTextEdit>
#include <QObject>
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
class LineNumberArea;
// Main text editor
class CodeEditor : public QPlainTextEdit
{
Q_OBJECT
public:
CodeEditor(QWidget *parent = 0);
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
protected:
void resizeEvent(QResizeEvent *event) override;
private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightCurrentLine();
void updateLineNumberArea(const QRect &, int);
private:
QWidget *lineNumberArea;
};
// Line number gutter
class LineNumberArea : public QWidget
{
public:
LineNumberArea(CodeEditor *editor) : QWidget(editor) {
codeEditor = editor;
}
QSize sizeHint() const override {
return QSize(codeEditor->lineNumberAreaWidth(), 0);
}
protected:
void paintEvent(QPaintEvent *event) override {
codeEditor->lineNumberAreaPaintEvent(event);
}
private:
CodeEditor *codeEditor;
};
#endif
codeeditor.cpp:
#include "codeeditor.h"
#include <QtWidgets>
#include <QFontMetrics>
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
lineNumberArea = new LineNumberArea(this);
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
updateLineNumberAreaWidth(0);
highlightCurrentLine();
setFixedSize(200, 200);
setMinimumSize(200,200);
}
int CodeEditor::lineNumberAreaWidth()
{
int digits = 1;
int max = qMax(1, blockCount());
while (max >= 10) {
max /= 10;
++digits;
}
//int space = 3 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits;
int space = 3 + 12 * digits;
return space;
}
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
{
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
{
if (dy)
lineNumberArea->scroll(0, dy);
else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0);
}
void CodeEditor::resizeEvent(QResizeEvent *e)
{
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
}
void CodeEditor::highlightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);
QTextBlock block = firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}
block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}
QMainWindow is a special widget since it has a preset layout
So you must set the main widget through setCentralWidget():
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
auto *widget_main = new QWidget;
auto *lay_main = new QVBoxLayout(widget_main);
auto *label = new QLabel("Test");
auto *editor = new CodeEditor();
setCentralWidget(widget_main); // <-- +++
// layout
lay_main->addWidget(label);
lay_main->addWidget(editor);
lay_main->setContentsMargins(5, 5, 5, 5);
}
On the other hand if you are going to use layouts then you should not set a fixed size to the widget, in your case remove setFixedSize(200, 200) on the other hand it is recommended that you make the connections with the new syntax:
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
{
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
lineNumberArea = new LineNumberArea(this);
connect(this, &QPlainTextEdit::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
connect(this, &QPlainTextEdit::updateRequest, this, &CodeEditor::updateLineNumberArea);
connect(this, &QPlainTextEdit::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine);
updateLineNumberAreaWidth(0);
highlightCurrentLine();
// setFixedSize(200, 200); <-- ---
setMinimumSize(200,200);
}

Qt / C++ - qDebug << class

How do I make qDebug print whether my class exists or not, or information about that class?? Can't believe there's nothing about this on the internet. I need to make sure that my ink = new InkSpot(this;) is actually returning something valid.
ink = new InkSpot(this);
qDebug << ink;
X:\Development\InkPuppet\inkpuppet.cpp:70: error: C3867: 'QMessageLogger::debug': function call missing argument list; use '&QMessageLogger::debug' to create a pointer to member
I've tried using QMessageLogger but just get various errors.
My program is crashing because I do something to ink.widget, so I'll post the code for ink
The code calling it is this:
header:
InkSpot *ink;
void InkPuppet::testButton()
{
ink = new InkSpot(this);
qDebug() << ink->widget;
ui->testButton->setText("working");
}
inkspot.h
#ifndef INKSPOT_H
#define INKSPOT_H
#include <QObject>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QLabel>
namespace Ui {
class InkSpot;
}
class InkSpot : public QWidget
{
Q_OBJECT
public:
explicit InkSpot(QWidget *parent = 0);
void draw(QPainter *painter);
QWidget *widget;
QLabel *label;
signals:
public slots:
protected:
void paintEvent(QPaintEvent *event);
private:
Ui::InkSpot *ui;
};
#endif // INKSPOT_H
inkspot.cpp
#include "inkspot.h"
#include "inkpuppet.h"
#include "ui_inkpuppet.h"
#include <QtCore>
#include <QtGui>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
InkSpot::InkSpot(QWidget *parent) :
QWidget(parent)
{
}
void InkSpot::paintEvent(QPaintEvent *event)
{
QFile *brushInput; //takes raw 8 bit grayscale image, 8 bit values only
char *brushProto;
uchar *brushData;
brushInput = new QFile("x:\\Development\\InkPuppet\\brush.raw"); //open the raw file
brushInput->open(QIODevice::ReadOnly);
QDataStream in;
in.setDevice(brushInput);
int size = brushInput->size(); //set size to length of raw file
brushProto = new char[size];
in.readRawData(brushProto, size); //read file into prototype
brushData = new uchar[size];
for(int i = 0; i < size; ++i)
{
brushData[i] = (uchar)brushProto[i]; //copy char to uchar array
}
QImage test(brushData, 128, 128, QImage::Format_Indexed8);
QImage test2(128, 128, QImage::Format_ARGB32);
QVector<QRgb> vectorColors(256); //create color table
for(int c = 0; c < 256; c++)
{
vectorColors[c] = qRgb(c, c, c);
}
test.setColorTable(vectorColors);
for(int iX = 0; iX < 100; ++iX)
{
for(int iY = 0; iY < 100; ++iY)
{
test2.setPixel(iX, iY, qRgba(255 - (qrand() % 100), 0 + (qrand() % 100), 0 + (qrand() % 100), qAbs((int)test.pixel(iX, iY)-255)));
}
}
//final conversion for stencil and color brush
QPixmap testPixmap = QPixmap::fromImage(test2);
QPixmap testPixmap2 = QPixmap::fromImage(test);
QPainter painter(this);
painter.drawPixmap(150, 50, 100, 100, testPixmap);
painter.drawPixmap(50, 50, 100, 100, testPixmap2);
delete[] brushProto;
delete[] brushData;
delete brushInput;
}
Not trivial what you want to do. That qDebug() displays something useful you have to implement the 'operator<<'. The following is a rough example, how this could look like:
QDebug inline operator<<(QDebug d, const InkSpot &f){
QDebug nsp = d.nospace();
nsp << f.label->text();
nsp << "\n";
return d;
}
This prints your InkSpot's label text. Add whatever else information your need. Put above code in your InkSpot.h file, but outside the class definition. If your need to access InkSpots private data, you must make QDebug operator<< a friend of InkSpot.