0

First I will describe what I want to do. I am using QT to build a window application. What I essentially wanted to do is say, I want a gridlayout which will be 10x10 and all the cells will be 5x5 but without having to populate the grid first! I populate the grid with my Widget class which extends the QWidget class. the Widget or QWidget has on the constructor a .resize(w,h) so i though this would restrain the w and h of the widget when loaded. However when i add a widget to the Grid it just goes in the center and the next where ever it wants to!

Furthermore inside the QWidget::Widget when its been clicked and blClicked = true then an image should be loaded.. but its not! *see code for details.

What I essentially want to achieve is this but with having 0 qwidgets in the qgridlayout and increasing them 1 by 1 sorting from bottom left to top left etc.

Thanks!

Code following:

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPainter>
#include "widget.h"
#include <iostream>


class Widget : public QWidget
{
private:
    Q_OBJECT
    int x,y,w,h;
    bool blClicked;
    QImage img;
public:
    explicit Widget(QWidget *parent = 0);
    Widget(int x,int y,int w);
    void paintEvent(QPaintEvent* event);
    void mousePressEvent (QMouseEvent* event );
    int getX();
    int getY();
    QWidget* toQWidget();
};

#endif // WIDGET_H

widget.cpp

Widget::Widget(int x, int y,int w)
{
    this->x = x;
    this->y = y;
    this->w = w;
    this->h = w;
    this->resize(this->w,this->h);
    blClicked = false;
    img.load("car.png");
    update();
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setWindow(x,y,w,h);
    painter.drawRect(x,y,w,h);
    if(blClicked)
    {
        std::cout << "add image" << std::endl;
        painter.drawImage(x,y,img);
    }
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    std::cout << "mouse click pressed" << std::endl;
    blClicked = !blClicked;
    update();
}

mainwindow.cpp

void MainWindow::on_btnCreateMap_clicked()
{
    using namespace std;
    if(ui->lnHeight->text() != NULL && ui->lnWidth->text() != NULL)
    {
       std::cout << "click" << std::endl;
       try {
             width = stoi(ui->lnWidth->text().toStdString());
             height = stoi(ui->lnHeight->text().toStdString());
             //Mapper m(height,width);
             l.push_back(new Widget(2,2,10));
             ui->gridWidget->addWidget(l[l.size()-1]);
             for (int i = 0; i < l.size(); i++)
             {
                 std::cout << "list size" << l.size() << std::endl;
                 std::cout << "address" << l[i] << std::endl;
             }

       }
       catch (exception& e)
       {
            cout << "Standard exception: " << e.what() << endl;
       }
    }
}
thuga
  • 12,601
  • 42
  • 52
czioutas
  • 1,032
  • 2
  • 11
  • 37
  • [QImage::load()](http://qt-project.org/doc/qt-5.0/qtgui/qimage.html#load) returns true if it's loaded successfully. You should check that, might be that your image is never loaded. – thuga Oct 21 '13 at 07:30
  • it failed to load the image because it was a .png file it works fine with a jpg – czioutas Oct 22 '13 at 15:09

0 Answers0