Saludos estoy aprendiendo QT y estaba viendo la clase QGraphicsItem y decidi hacer programa despues de un par de tutoriales pero cree un desastre,
lo que se supone que hace el programa es:
cuando doy click en un cuadrado este se borra y se acumulan 50 puntos, pero lo que tengo es un nuevo screensaver![:laugh: :laugh:](https://forum.elhacker.net/Smileys/navidad/laugh.gif)
![](http://www.mediafire.com/conv/49a14586204d09932512796c6b559516a67d387e55e6c777eee527b17c18d2bb6g.jpg)
hghdjddjdjd.pro
mainwindow.h
cuadrado.h
mainwindow.cpp
cuadrado.cpp
main.cpp
mainwindow.ui
aqui les dejo un archivo con todo el proyecto junto por si les es mas facil ayudarme asi :
http://www.mediafire.com/?8db8cetvm9xsfgs
lo que se supone que hace el programa es:
cuando doy click en un cuadrado este se borra y se acumulan 50 puntos, pero lo que tengo es un nuevo screensaver
![:laugh: :laugh:](https://forum.elhacker.net/Smileys/navidad/laugh.gif)
![](http://www.mediafire.com/conv/49a14586204d09932512796c6b559516a67d387e55e6c777eee527b17c18d2bb6g.jpg)
hghdjddjdjd.pro
Código [Seleccionar]
#-------------------------------------------------
#
# Project created by QtCreator 2012-10-13T00:07:08
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = hghdjddjdjd
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
cuadrado.cpp
HEADERS += mainwindow.h \
cuadrado.h
FORMS += mainwindow.ui
mainwindow.h
Código (c) [Seleccionar]
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore>
#include <QtGui>
#include "cuadrado.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
cuadrado *square[30];
};
#endif // MAINWINDOW_H
cuadrado.h
Código (c) [Seleccionar]
#ifndef CUADRADO_H
#define CUADRADO_H
#include <QPainter>
#include <QGraphicsItem>
#include <QDebug>
class cuadrado : public QGraphicsItem
{
public:
cuadrado();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
int points;
bool Pressed;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
};
#endif // CUADRADO_H
mainwindow.cpp
Código (c) [Seleccionar]
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
for (int i = 0; i < 29; i++) {
square[i] = new cuadrado();
scene->addItem(square[i]);
}
}
MainWindow::~MainWindow()
{
delete ui;
}
cuadrado.cpp
Código (c) [Seleccionar]
#include "cuadrado.h"
cuadrado::cuadrado()
{
Pressed = false;
points = 0;
}
QRectF cuadrado::boundingRect() const
{
return QRectF(rand() % 1000,rand() % 500,100,100);
}
void cuadrado::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QRectF *rec = new QRectF;
*rec = boundingRect();
QBrush brush (Qt::blue);
painter->fillRect(*rec,brush);
painter->drawRect(*rec);
// update(); //aparente el esta tomando la orden como falsa ??
if(Pressed) {
painter->eraseRect(*rec);
qDebug() << Pressed;
points += 50;
} else {qDebug() << Pressed; }
update();
}
void cuadrado::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Pressed = true;
qDebug() << Pressed;
update();
QGraphicsItem::mouseMoveEvent(event);
}
void cuadrado::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Pressed = false;
qDebug() << Pressed;
update();
QGraphicsItem::mouseReleaseEvent(event);
}
main.cpp
Código (c) [Seleccionar]
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.ui
Código (xml) [Seleccionar]
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1280</width>
<height>720</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QGraphicsView" name="graphicsView">
<property name="geometry">
<rect>
<x>20</x>
<y>10</y>
<width>1241</width>
<height>641</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1280</width>
<height>16</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
aqui les dejo un archivo con todo el proyecto junto por si les es mas facil ayudarme asi :
http://www.mediafire.com/?8db8cetvm9xsfgs