Please can you help me, write a function to query or set the state for checked button? It would be nice if i can build the grid a little bit dynamic in size!
I have subclass a QWidget to build a button grid. I build the grid like this:
layout->setMargin(3);
layout->setSpacing(0);
// Create cells
for (int i = 0; i < 9; ++i) {
int col = (i % 3) * 3;
int max_col = col + 3;
int row = (i / 3) * 3;
int max_row = row + 3;
box->setMargin(2);
box->setSpacing(1);
layout->addLayout(box, row / 3, col / 3);
for (int r = row; r < max_row; ++r) {
for (int c = col; c < max_col; ++c) {
//Cell* cell = new Cell(c, r, this, this);
cell->setCheckable(true);
cell->setAutoExclusive(true);
box->addWidget(cell, r - row, c - col);
m_cells[c][r] = cell;
}
}
}
QGridLayout* layout = new QGridLayout(this);
layout->setMargin(3);
layout->setSpacing(0);
// Create cells
for (int i = 0; i < 9; ++i) {
int col = (i % 3) * 3;
int max_col = col + 3;
int row = (i / 3) * 3;
int max_row = row + 3;
QGridLayout* box = new QGridLayout;
box->setMargin(2);
box->setSpacing(1);
layout->addLayout(box, row / 3, col / 3);
for (int r = row; r < max_row; ++r) {
for (int c = col; c < max_col; ++c) {
//Cell* cell = new Cell(c, r, this, this);
QPushButton* cell = new QPushButton(this);
cell->setCheckable(true);
cell->setAutoExclusive(true);
box->addWidget(cell, r - row, c - col);
m_cells[c][r] = cell;
}
}
}
To copy to clipboard, switch view to plain text mode
Here is my solution for all other user to rotate a QLabel:
myLabel.h
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>
class MyLabel
: public QLabel{ Q_OBJECT
public:
~MyLabel();
bool rotateText(float degrees);
private:
float rotation;
protected:
void drawRotatedText
(QPainter *painter,
float degrees,
int x,
int y,
const QString &text
);
};
#endif
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>
class MyLabel : public QLabel{
Q_OBJECT
public:
MyLabel(QWidget *parent = 0);
~MyLabel();
bool rotateText(float degrees);
private:
float rotation;
protected:
void paintEvent(QPaintEvent *);
void drawRotatedText(QPainter *painter, float degrees, int x, int y, const QString &text);
};
#endif
To copy to clipboard, switch view to plain text mode
myLabel.cpp:
#include "myLabel.h"
#include <QPainter>
{
this->rotateText(NULL);
}
bool MyLabel::rotateText(float degrees)
{
if (degrees >=0 && degrees <= 360)
{
rotation=degrees;
update();
return TRUE;
}
return FALSE;
}
{
p.begin( this );
p.setFont(font());
drawRotatedText(&p, rotation, width() / 2, height() / 2, text());
p.end();
}
void MyLabel
::drawRotatedText(QPainter *painter,
float degrees,
int x,
int y,
const QString &text
) {
painter->save();
painter->translate(x, y);
painter->rotate(degrees);
painter->drawText(0, 0, text);
painter->restore();
}
#include "myLabel.h"
#include <QPainter>
MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
this->rotateText(NULL);
}
bool MyLabel::rotateText(float degrees)
{
if (degrees >=0 && degrees <= 360)
{
rotation=degrees;
update();
return TRUE;
}
return FALSE;
}
void MyLabel::paintEvent(QPaintEvent *)
{
QPainter p;
p.begin( this );
p.setFont(font());
drawRotatedText(&p, rotation, width() / 2, height() / 2, text());
p.end();
}
void MyLabel::drawRotatedText(QPainter *painter, float degrees, int x, int y, const QString &text)
{
painter->save();
painter->translate(x, y);
painter->rotate(degrees);
painter->drawText(0, 0, text);
painter->restore();
}
To copy to clipboard, switch view to plain text mode
Bookmarks