Sunday, July 6th 2008, 5:02pm UTC+1

You are not logged in.

  • Login
  • Register

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

dolby

Beginner

Posts: 9

Location: Finland

Occupation: IT Consultant

1

Friday, May 16th 2008, 9:09am

Grid, Table...

Hi I am very much new to this Qt though i have little experience with Gtk. Ok this is what i want to do. I want to construct a 5x5 table where i can place a picture or button. When any of the picture or button is pressed i need to know the matrix co-ordinate of the same.

I tried using this GTableWidget but the problem is i get the default row/column name and even if i delete those the default numbering happens, which i don't need.

I tried using the QGrid but don't know exactly how i can return the co-ordinate where the "clicked" event happens.

so what all i need is a 5x5 table which could be invisible where i can add widget like picture or button and most importantly i need the matrix co-ordinate when one the picture/button is pressed.

It might be really very silly doubt but any help would be highly appreciated.

Br
Dolby
  • Go to the top of the page

2

Friday, May 16th 2008, 9:36am

RE: Grid, Table...

have a look at QSignalMapper documentation
Nicolas
  • Go to the top of the page

dolby

Beginner

Posts: 9

Location: Finland

Occupation: IT Consultant

3

Friday, May 16th 2008, 9:52am

RE: Grid, Table...

Hi Nicholas,

Thnx for the info. I will look into that. But what abt the table without those headers? Is it possible to use Grid and identify the co-ordinate where it's clicked?

Br
Dolby
  • Go to the top of the page

4

Friday, May 16th 2008, 10:09am

RE: Grid, Table...

what table?

for QTableView yes:
signal void clicked( const QModelIndex & index )

but I think the problem is not is it possible to know the coordinates where you click
but do you need model / view to do what you want
Nicolas
  • Go to the top of the page

dolby

Beginner

Posts: 9

Location: Finland

Occupation: IT Consultant

5

Friday, May 16th 2008, 10:18am

RE: Grid, Table...

Hi to make it clear, this is what i want to start off with Qt. A game called LIGHTS OFF already available in iPhone. http://www.macrumors.com/2007/08/13/ligh…ve-iphone-game/

Though i have all the logic ready in pure C consider it bcos of the lack of knowledge in C++ i find it bit difficult. In steps this is what i want to do.

1. Have a grid/table which could be invisible

2. Add a picture/button to each cell in the grid/table

3. When any of the cell is "clicked" get the co-ordinate of the cell and accordingly change the picture/button of the other cell's.

Hope this makes clear.

Br
Dolby
  • Go to the top of the page

dolby

Beginner

Posts: 9

Location: Finland

Occupation: IT Consultant

6

Friday, May 16th 2008, 10:21am

RE: Grid, Table...

In case you want to see it in action check here

http://lucasnewman.com/lightsout2.mp4

So in simple terms i am trying to do this game with Qt. I have done it with Gtk and its working fine.
Dolby
  • Go to the top of the page

stinny

Beginner

Posts: 26

Location: Prague

Occupation: CS student

7

Friday, May 16th 2008, 10:59am

RE: Grid, Table...

I would personally generate 25 QPushButtons (QLabel etc. - you may find which is appropriate) and position them in QGridLayout (it is easy to use and transparent).
For the purposes of signals, create a QSignalMapper and connect it with something like this

Source code

1
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
to each button. Then, with the function setMapping assign to each QPushButton an int (or QString) which stores the position. Then, you can connect the signal QSignalMapper::mapped(int) to any slot (method) which will handle the click "event".

As You're new to Qt, it could be a bit hard for You, I recommend You to study the Signals and Slots and Layout management pages as well as the pages of all classes mentioned above (in QtAssistant). It might be also good idea to start with Tutorial and some related Examples. If You get stuck, feel free to ask here.

This post has been edited 1 times, last edit by "stinny" (May 16th 2008, 11:01am)

  • Go to the top of the page

8

Friday, May 16th 2008, 11:01am

RE: Grid, Table...

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class ButtonWidget : public QWidget
{
    Q_OBJECT
public:
    ButtonWidget( QWidget * p = NULL );
signals:
    void clicked( int num );
private:
    QSignalMapper * mapper;
 };

ButtonWidget::ButtonWidget( QWidget * p ) : QWidget( p )
{
    mapper             = new QSignalMapper( this );
    QGridLayout * grid = new QGridLayout( this );
    for( int i = 0; i < 5 * 5; ++i )
    {
        QPushButton * button = new QPushButton();
        connect( button, SIGNAL(clicked()), mapper, SLOT(map()) );
        mapper->setMapping( button, i );
        grid->addWidget( button, i / 5, i % 5);
    }
    connect( mapper, SIGNAL(mapped(int)), this, SIGNAL(clicked(int)));
}

connect slot clicked(int) of your ButtonWidget to some slot where you use i/5 and i%5 to determine the column/row clicked
Nicolas
  • Go to the top of the page

dolby

Beginner

Posts: 9

Location: Finland

Occupation: IT Consultant

9

Friday, May 16th 2008, 11:03am

Hi,

That was so nice of you. Yes i am already going thru the tutorial and its really tough to go thru all the classes and stuff. May be since i am new i feel it tough. But on the other hand i do have another to work around. I will go through what ever classes u hav mentioned and will sure get back if i am stuck.

Br
Dolby
  • Go to the top of the page

dolby

Beginner

Posts: 9

Location: Finland

Occupation: IT Consultant

10

Friday, May 16th 2008, 11:06am

RE: Grid, Table...

Hi,

Yes. This was the work around i too have. :-) will do some R&D this weekend and will get back on how it went.

Br
Dolby
  • Go to the top of the page

Rate this thread