PDA

View Full Version : Please Help with QButton



agup006
19th November 2011, 05:02
I just want to create a button that will allow me to paint on an image i already have made in my main class. I want this button to draw a square on my image, but whenever i try and use it i cant have it redrawn on my label.


#include <QtCore>
#include <QtGui>
#include <QDebug>
#include <CoordinateValue.h>
#include <mainwindow.h>
#include <Level.h>
//#include <Coordinate.h>

//#include<Graphics.h>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QLabel window;
QPixmap lvlone;
QPixmap ftowpic;
QIcon ftowicon;

lvlone.load(QString::fromUtf8("C:/Users/Agup/Desktop/Map1.bmp"));
ftowpic.load(QString::fromUtf8("C:/Users/Agup/Desktop/FreshmenTower.bmp"));

CoordinateValue cv;
//cv.setValid(1);

//load pixmap into icon for button
ftowicon.addPixmap(ftowpic);
QSize ftowsize(25,25);


// next code is to check if worked or not
//qDebug() << goodLoad << lvlone.isNull() << lvlone.size() << lvlone.depth();

//make mappaint use the map as background


QPainter mappaint(&lvlone);


QPen Black((QColor::QColor(0,0,0)),1);
mappaint.setPen(Black);

for(int i = 0;i<501;i += 50)
{
mappaint.drawLine(0,i,500,i);
}
for(int j = 0;j<501;j += 50)
{
mappaint.drawLine(j,0,j,500);
}
//use this code to draw towers depenrding on valid spots.
//mappaint.drawPixmap(200,150,50,50, ftowpic);



//QPushButton Start("Start", &window);
//Start.setGeometry(200,200,50,50);
//QObject::connect(&Start,SIGNAL(clicked()),g,SLOT(PaintIT()));


//freshmen tower button
QPushButton FTower(ftowicon,"Freshmen\n Tower", &window);
FTower.setFont(QFont("Purisa",8));
FTower.setGeometry(500,0,100,100);
FTower.setIconSize(ftowsize);



cv.getPainter(mappaint);

QObject::connect(&FTower,SIGNAL(clicked()),&cv, SLOT(paintValid()));
QPushButton quit("Quit", &window);
quit.setFont(QFont("Purisa", 12));
quit.setGeometry(575,400,100,50);
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));

window.resize(750,500);
window.setPixmap(lvlone);
window.show();

cv.paintValid();
return app.exec();
}


and it uses a slot from here


#ifndef COORDINATEVALUE_H
#define COORDINATEVALUE_H


#include <QtCore>
#include <QtGui>
#include <QDebug>
#include <Coordinate.h>
class CoordinateValue: public QObject
{
Q_OBJECT

Coordinate coor[100];

public:
QPixmap newImage;
QLabel newWindow;
QPainter plzwrk;

//makes 100 coordinates

int getSpot(int x,int y)
{

//returns numerical spot corresponding
//with array coor for which spot coordinate is in
return (x%50)+((y/50)*10);
}
void setValues()
{
//sets each value of grid in image
//to a value of 1-100 based on location
int c = 0;
for(int i = 0; i<500; i+= 50){
for(int j = 0;j<500; j+=50)
{
coor[c].setX(j);
coor[c].setY(i);
c++;
}
}
}
//get X Drawing Coordinates
int getXDC(int spot)
{
return coor[spot].getX();
}
//get Y Drawing Coordinates
int getYDC(int spot)
{
return coor[spot].getY();
}
void setValid(int levelnum)
{
// switch (levelnum)
// case 1:

// break;
// case 2:

// break;

}
void getPainter(QPainter &painter)
{
plzwrk = painter;
}
public slots:
void paintValid()
{
QPen linepen;
linepen.setWidth(2);
linepen.setColor(Qt::green);
plzwrk.setPen(linepen);

QPolygon poly;
poly<< QPoint(0,0);
poly<< QPoint(0,50);
poly<< QPoint(50,50);
poly<< QPoint(50,0);

QBrush transbrush;
transbrush.setColor(Qt::green);
transbrush.setStyle(Qt::Dense5Pattern);

QPainterPath path;
path.addPolygon(poly);

plzwrk.drawPolygon(poly);
plzwrk.fillPath(path,transbrush);

}
void doNothing()
{
QString new1 = "hi";
qDebug()<<new1;
}

};
#endif // COORDINATEVALUE_H






i get that its not neat but any help would greatly be appreciated thanks

grin
19th November 2011, 13:02
I just want to create a button that will allow me to paint on an image

For this task you do not necessarily create you own button's class. You can you QPushButton or QToolButton.
See http://doc.qt.nokia.com/stable/qabstractbutton.html#icon-prop.

P.S. - I don't saw your code - sorry.