Hi I am using QWidget
mainWindow.h
ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
class CharacterWidget;
{
Q_OBJECT
public:
MainWindow();
public slots:
void fillGrid();
private:
CharacterWidget *characterWidget;
};
#endif
ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
class QClipboard;
class QComboBox;
class QFontComboBox;
class QLineEdit;
class QScrollArea;
class QCheckBox;
class CharacterWidget;
class QPushButton;
class QPaintEvent;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
public slots:
void fillGrid();
private:
CharacterWidget *characterWidget;
QClipboard *clipboard;
QScrollArea *scrollArea;
QPushButton *button;
};
#endif
To copy to clipboard, switch view to plain text mode
characterwidget.h
#ifndef CHARACTERWIDGET_H
#define CHARACTERWIDGET_H
#include <QFont>
#include <QPoint>
#include <QSize>
#include <QString>
#include <QWidget>
class CharacterWidget
: public QWidget{
Q_OBJECT
public:
CharacterWidget
(QWidget *parent
= 0);
protected:
private:
int columns;
int lastKey;
int squareSize;
public:
int irow;
int icol;
void FillColor();
};
#endif
#ifndef CHARACTERWIDGET_H
#define CHARACTERWIDGET_H
#include <QFont>
#include <QPoint>
#include <QSize>
#include <QString>
#include <QWidget>
class QPaintEvent;
class CharacterWidget : public QWidget
{
Q_OBJECT
public:
CharacterWidget(QWidget *parent = 0);
QSize sizeHint() const;
void paintEvent(QPaintEvent *event);
protected:
void mouseMoveEvent(QMouseEvent *event);
private:
int columns;
int lastKey;
int squareSize;
public:
QTimer *timer;
int irow;
int icol;
void FillColor();
};
#endif
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow window;
window.show();
return app.exec();
}
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include <QtGui>
#include "characterwidget.h"
#include "mainwindow.h"
MainWindow::MainWindow()
{
characterWidget = new CharacterWidget;
scrollArea->setWidget(characterWidget);
controlsLayout->addStretch(1);
lineLayout->addSpacing(12);
centralLayout->addLayout(controlsLayout);
centralLayout->addWidget(scrollArea, 1);
centralLayout->addWidget(button, 1);
centralLayout->addSpacing(4);
centralLayout->addLayout(lineLayout);
centralWidget->setLayout(centralLayout);
setCentralWidget(centralWidget);
connect(button,SIGNAL(clicked()),this,SLOT(fillGrid()));
}
void MainWindow::fillGrid()
{
characterWidget->FillColor();
}
#include <QtGui>
#include "characterwidget.h"
#include "mainwindow.h"
MainWindow::MainWindow()
{
QWidget *centralWidget = new QWidget;
scrollArea = new QScrollArea;
button = new QPushButton();
characterWidget = new CharacterWidget;
scrollArea->setWidget(characterWidget);
clipboard = QApplication::clipboard();
QHBoxLayout *controlsLayout = new QHBoxLayout;
controlsLayout->addStretch(1);
QHBoxLayout *lineLayout = new QHBoxLayout;
lineLayout->addSpacing(12);
QVBoxLayout *centralLayout = new QVBoxLayout;
centralLayout->addLayout(controlsLayout);
centralLayout->addWidget(scrollArea, 1);
centralLayout->addWidget(button, 1);
centralLayout->addSpacing(4);
centralLayout->addLayout(lineLayout);
centralWidget->setLayout(centralLayout);
setCentralWidget(centralWidget);
connect(button,SIGNAL(clicked()),this,SLOT(fillGrid()));
}
void MainWindow::fillGrid()
{
characterWidget->FillColor();
}
To copy to clipboard, switch view to plain text mode
characterwidget.cpp
#include <QtGui>
#include "characterwidget.h"
#include "mainwindow.h"
bool flag=false;
CharacterWidget
::CharacterWidget(QWidget *parent
){
squareSize = 16;
columns = 16;
irow =0;
icol =0;
setMouseTracking(true);
}
QSize CharacterWidget
::sizeHint() const {
return QSize(columns
*squareSize, columns
*squareSize
);
}
void CharacterWidget
::mouseMoveEvent(QMouseEvent *event
) {
QPoint widgetPosition
= mapFromGlobal
(event
->globalPos
());
uint key = (widgetPosition.y()/squareSize)*columns + widgetPosition.x()/squareSize;
QToolTip::showText(event
->globalPos
(), text,
this);
}
void CharacterWidget::FillColor()
{
flag=true;
update();
}
{
painter.
fillRect(event
->rect
(),
QBrush(Qt
::white));
QRect redrawRect
= event
->rect
();
int beginRow = redrawRect.top()/squareSize;
int endRow = redrawRect.bottom()/squareSize;
int beginColumn = redrawRect.left()/squareSize;
int endColumn = redrawRect.right()/squareSize;
painter.
setPen(QPen(Qt
::gray));
for (int row = beginRow; row <= endRow;++row)
{
for (int column = beginColumn; column <= endColumn; ++column)
{
painter.drawRect(column*squareSize, row*squareSize, squareSize, squareSize);
}
}
painter.
setPen(QPen(Qt
::black));
if(flag == true)
{
int row = irow;
{
for (int column =icol; column<=endColumn;++column)
{
painter.setClipRect(column*squareSize, row*squareSize, squareSize, squareSize);
painter.
fillRect(column
*squareSize
+ 1, row
*squareSize
+ 1, squareSize, squareSize,
QBrush(Qt
::green));
icol++;
if(icol==16)
{
icol=0;
irow++;
}
}
}
}
}
#include <QtGui>
#include "characterwidget.h"
#include "mainwindow.h"
bool flag=false;
CharacterWidget::CharacterWidget(QWidget *parent)
: QWidget(parent)
{
squareSize = 16;
columns = 16;
irow =0;
icol =0;
setMouseTracking(true);
}
QSize CharacterWidget::sizeHint() const
{
return QSize(columns*squareSize, columns*squareSize);
}
void CharacterWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint widgetPosition = mapFromGlobal(event->globalPos());
uint key = (widgetPosition.y()/squareSize)*columns + widgetPosition.x()/squareSize;
QString text = QString::number(key,10);
QToolTip::showText(event->globalPos(), text, this);
}
void CharacterWidget::FillColor()
{
flag=true;
update();
}
void CharacterWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPainterPath myPath;
painter.fillRect(event->rect(), QBrush(Qt::white));
QRect redrawRect = event->rect();
int beginRow = redrawRect.top()/squareSize;
int endRow = redrawRect.bottom()/squareSize;
int beginColumn = redrawRect.left()/squareSize;
int endColumn = redrawRect.right()/squareSize;
painter.setPen(QPen(Qt::gray));
for (int row = beginRow; row <= endRow;++row)
{
for (int column = beginColumn; column <= endColumn; ++column)
{
painter.drawRect(column*squareSize, row*squareSize, squareSize, squareSize);
}
}
painter.setPen(QPen(Qt::black));
if(flag == true)
{
int row = irow;
{
for (int column =icol; column<=endColumn;++column)
{
painter.setClipRect(column*squareSize, row*squareSize, squareSize, squareSize);
painter.fillRect(column*squareSize + 1, row*squareSize+ 1, squareSize, squareSize, QBrush(Qt::green));
icol++;
if(icol==16)
{
icol=0;
irow++;
}
}
}
}
}
To copy to clipboard, switch view to plain text mode
Please review my code .
Thanx
Bookmarks