Is there any function which I can use in order to pad my QImage object?
I have tried to search over the net unsuccefuly.
Thx in advance.
Here is my code:
#include "mainwindow.h"
#include <QApplication>
#include "qimage.h"
#include <QImage>
#include <QLabel>
#include <QColor>
#include "qcolor.h"
#include <Qdebug>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QGraphicsView>
int main(int argc, char *argv[])
{
printf("Init!");
qDebug() << "C++ Style Debug Message";
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
int height;
int width;
unsigned char *p, *p_begin;
QImage img("C:\\Users\\Owner\\Pictures\\2013-09-26\\IMG_0836.JPG");
height = img.height();
width = img.width();
p = (unsigned char *)malloc(height * width * sizeof(unsigned char));
p_begin = p;
qDebug() << "Begin For Loop";
for (int row = 0; row < height; ++row)
{
for (int col = 0; col < width; ++col)
{
QColor clrCurrent( img.pixel( col, row ));
*p = (unsigned char)((clrCurrent.green() * 0.587) + (clrCurrent.blue() * 0.114) + (clrCurrent.red() * 0.299));
p++;
}
}
qDebug() << "Finished First Loop!";
p = p_begin;
for (int row = 0; row < height; ++row)
{
for (int col = 0; col < width; ++col)
{
QColor clrCurrent(img.pixel(col, row));
clrCurrent.setBlue((int)(*p));
clrCurrent.setGreen((int)(*p));
clrCurrent.setRed((int)(*p));
img.setPixel(col, row, clrCurrent.rgba());
p++;
}
}
QPixmap pixmap = QPixmap::fromImage(img);
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
scene.addItem(item);
view.show();
return a.exec();
}
Hi I edited my question, i added my code in order to give you more feeling of what is going on. any help would be appreciated.
The QImage offers no way to change its size. You need to create a new, larger image, erase its contents, start a QPainter on it, then draw the source image in the center of the new image. That way you'll have padding.
Below is a function that returns a padded version of the image, with a given color used for padding, and a test harness for it.
// https://github.com/KubaO/stackoverflown/tree/master/questions/image-pad-35968431
#include <QtGui>
template <typename T>
QImage paddedImage(const QImage & source, int padWidth, T padValue) {
QImage padded{source.width() + 2*padWidth, source.height() + 2*padWidth, source.format()};
padded.fill(padValue);
QPainter p{&padded};
p.drawImage(QPoint(padWidth, padWidth), source);
return padded;
}
int main() {
QImage source{64, 64, QImage::Format_ARGB32_Premultiplied};
source.fill(Qt::red);
auto padded = paddedImage(source, 16, Qt::blue);
padded.save("test.png");
}
Output:
Related
In a QTableView, irrespective if there are entries or not. I want to show grid to it (considering its fixed size QTableView), but only for columns, not for rows.
You have to overwrite the paintEvent() method, in addition to setting the setShowGrid() to false.
#include <QApplication>
#include <QPainter>
#include <QStandardItemModel>
#include <QTableView>
#include <QHeaderView>
#include <QPaintEvent>
class TableView: public QTableView{
public:
using QTableView::QTableView;
protected:
void paintEvent(QPaintEvent *event){
QTableView::paintEvent(event);
if (horizontalHeader()->count() == 0 || verticalHeader()->count() == 0)
return;
QPainter painter(viewport());
QStyleOptionViewItem option;
option.init(this);
const int gridHint = style()->styleHint(QStyle::SH_Table_GridLineColor, &option, this);
const QColor gridColor = static_cast<QRgb>(gridHint);
const QPen gridPen = QPen(gridColor, 0, gridStyle());
painter.setPen(gridPen);
int w = horizontalHeader()->offset();
for(int i=0; i<horizontalHeader()->count(); ++i){
w += horizontalHeader()->sectionSize(i);
painter.drawLine(w-1, 0, w-1,viewport()->height());
}
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TableView view;
QStandardItemModel model(5, 5);
for(int i=0; i< model.rowCount(); ++i)
for(int j=0; j < model.columnCount(); ++j)
model.setItem(i, j, new QStandardItem(QString("%1-%2").arg(i).arg(j)));
view.setModel(&model);
view.setShowGrid(false);
view.show();
return a.exec();
}
Hi I've got a GridLayout which has 64 GraphicsViews on it (I know it's alot but it's the only way i could think of doing this at this point in time).
Now i'm currently just drawing a random line on each of these graphics views on a timer tick. This works but only for the 8 of the Graphics,
Create Graphics Views
void Simulation::createGraphicsViews(){
for(int i = 0; i < 64; i++){
for(int j = 0; j < 8; j++){
graphicsScene[i] = new QGraphicsScene();
graphicsView[i] = new QGraphicsView(graphicsScene[i]);
simui->gridLayout->addWidget(graphicsView[i], i/8, j);
}
}
}
Random Line in each graphics view
for(int x = 0; x < 64; x++){
x1 = qrand()%(50+1) - 1;
y1 = qrand()%(50+1)-1;
x2 = qrand()%(50+1)-1;
y2 = qrand()%(50+1)-1;
graphicsScene[x]->addLine(x1,y1,x2,y2);
qDebug() << "adding line to" << x << "at" << x1 <<","<<y1<<","<<x2<<","<<y2;
}
show updated graphics view
for(int x = 0; x < 64; x++){
graphicsView[x]->show();
qDebug()<<"showing" << x;
}
I've looked through it for the last 2 hours trying multiple approaches none of which have fixed this problem, I'm assuming it's probably something stupid but I just can't figure it out
Any help is greatly appreciated
Thank you
Also if i try to update any of the Graphics Views other than the ones which work they still don't update.
https://gist.github.com/gazza126/f43d5b0377649782a35d -- Full Code (that does anything)
The below works. Make sure that you enable C++11 in your .pro file: add CONFIG += c++11 to the project file.
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsLineItem>
#include <QGridLayout>
#include <QTime>
#include <QTimer>
#include <array>
class View : public QGraphicsView
{
public:
View(QWidget *parent = 0) : QGraphicsView(parent) {
setRenderHint(QPainter::Antialiasing);
}
void resizeEvent(QResizeEvent *) {
fitInView(-1, -1, 2, 2, Qt::KeepAspectRatio);
}
};
template <typename Container>
void updateScenes(Container & views)
{
auto angle = 360.0/1000.0 * (QTime::currentTime().msecsSinceStartOfDay() % 1000);
for (auto & view : views) {
auto scene = view.scene();
scene->clear();
auto * line = scene->addLine(-1, 0, 1, 0, QPen(Qt::darkBlue, 0.1));
line->setRotation(angle);
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene s;
QTimer timer;
QWidget window;
QGridLayout layout(&window);
std::array<View, 64> views;
int i = 0;
for (auto & view : views) {
view.setScene(new QGraphicsScene(&view));
layout.addWidget(&view, i/8, i%8);
++ i;
}
QObject::connect(&timer, &QTimer::timeout, [&views]{ updateScenes(views); });
timer.start(50);
window.show();
return a.exec();
}
I have an 2D array QPushButton, how can I get index of the button when user clicks on its? such as When user clicks on the button a[2][3] it will show (2,3) ?
The example looks like this:
Qt 4/5 Using Object Names
You can give your buttons unique object names. The names should ideally be valid C++ identifiers.
// https://github.com/KubaO/stackoverflown/tree/master/questions/button-grid-22641306
#include <QtGui>
#if QT_VERSION_MAJOR >= 5
#include <QtWidgets>
#endif
struct Display : QLabel {
Q_SLOT void onClicked() {
auto const elements = sender()->objectName().split('_');
auto const i = elements.at(1).toInt();
auto const j = elements.at(2).toInt();
setText(QString{"(%1,%2)"}.arg(i).arg(j));
}
Q_OBJECT
};
int main(int argc, char *argv[])
{
QApplication a{argc, argv};
QWidget window;
QGridLayout layout{&window};
QVarLengthArray<QPushButton, 12> buttons(12);
Display display;
const int rows = 4, columns = 3;
for (int i = 0; i < rows; ++ i)
for (int j = 0; j < columns; ++j) {
auto & button = buttons[i*columns+j];
button.setText(QString{"(%1,%2)"}.arg(i).arg(j));
button.setObjectName(QString{"buton_%1_%2"}.arg(i).arg(j));
layout.addWidget(&button, i, j);
display.connect(&button, SIGNAL(clicked()), SLOT(onClicked()));
}
layout.addWidget(&display, rows, 0, 1, columns);
window.show();
return a.exec();
}
#include "main.moc"
Qt 5 - Using Lambdas
In Qt 5 and C++11, you should use functors to generate custom slot for each button, on the fly. For example:
// https://github.com/KubaO/stackoverflown/tree/master/questions/button-grid-22641306
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a{argc, argv};
QWidget window;
QGridLayout layout{&window};
QVarLengthArray<QPushButton, 12> buttons(12);
QLabel display;
const int rows = 4, columns = 3;
for (int i = 0; i < rows; ++ i)
for (int j = 0; j < columns; ++j) {
auto text = QStringLiteral("(%1,%2)").arg(i).arg(j);
auto & button = buttons[i*columns+j];
button.setText(text);
layout.addWidget(&button, i, j);
QObject::connect(&button, &QPushButton::clicked, [&display, text] {
display.setText(text);
});
}
layout.addWidget(&display, rows, 0, 1, columns);
window.show();
return a.exec();
}
Qt 4/5 - Using QSignalMapper
QSignalMapper is pretty much designed for what you want. It lets you map a QObject* to "something else", like a string. For example:
#include <QtGui>
#if QT_VERSION_MAJOR >= 5
#include <QtWidgets>
#endif
int main(int argc, char *argv[])
{
QApplication a{argc, argv};
QSignalMapper mapper;
QWidget window;
QGridLayout layout{&window};
QVarLengthArray<QPushButton, 12> buttons(12);
QLabel display;
const int rows = 4, columns = 3;
for (int i = 0; i < rows; ++ i)
for (int j = 0; j < columns; ++j) {
auto text = QString{"(%1,%2)"}.arg(i).arg(j);
auto & button = buttons[i*columns+j];
button.setText(text);
layout.addWidget(&button, i, j);
mapper.connect(&button, SIGNAL(clicked()), SLOT(map()));
mapper.setMapping(&button, text);
}
display.connect(&mapper, SIGNAL(mapped(QString)), SLOT(setText(QString)));
layout.addWidget(&display, rows, 0, 1, columns);
window.show();
return a.exec();
}
Qt 4/5 - Using the Property System
You can leverage the fact that a QWidget is a QObject. QObjects have a property system, so you can set each button's index as a property, and then retrieve it in the slot connected to the clicked() signal. For example:
#include <QtGui>
#if QT_VERSION_MAJOR >= 5
#include <QtWidgets>
#endif
const char kIndex[] = "index";
struct Display : QLabel {
Q_SLOT void onClicked() {
setText(sender()->property(kIndex).toString());
}
Q_OBJECT
};
int main(int argc, char *argv[])
{
QApplication a{argc, argv};
QWidget window;
QGridLayout layout{&window};
QVarLengthArray<QPushButton, 12> buttons(12);
Display display;
const int rows = 4, columns = 3;
for (int i = 0; i < rows; ++ i)
for (int j = 0; j < columns; ++j) {
auto index = QString{"(%1,%2)"}.arg(i).arg(j);
auto & button = buttons[i*columns+j];
button.setText(index);
button.setProperty(kIndex, index);
layout.addWidget(&button, i, j);
display.connect(&button, SIGNAL(clicked()), SLOT(onClicked()));
}
layout.addWidget(&display, rows, 0, 1, columns);
window.show();
return a.exec();
}
#include "main.moc"
in general, you would have to loop over the array and test the event target for equality with each element until you reach the correct index
I've been working on my UI, and all that stuff compiles fine but I also wrote up the majority of my code then generated the header file for it part way through. But this header file will not compile - no errors. It just isn't in the build directory at all.
ink.h
#ifndef INK_H
#define INK_H
#include <QPaintDevice>
namespace Ui {
class Ink;
}
class Ink : public QPaintDevice
{
public:
explicit Ink(QWidget *parent = 0);
~Ink();
private:
Ui::Ink *ui;
};
#endif // INK_H
ink.cpp
#include "ink.h"
#include <QtCore>
#include <QtGui>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
void ink()
{
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;
}
main.cpp
#include "inkpuppet.h"
#include "ink.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
InkPuppet w;
w.show();
return a.exec();
}
When you create a new form in QT creator you have to choose Qt Designer Form Class in order to generate the corresponding .cpp and .h files, otherwise it will only generate the .ui.
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.