You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Michiel

Trainee

  • "Michiel" is male
  • "Michiel" started this thread

Posts: 112

Location: The Netherlands

Occupation: Student / Programmer

  • Send private message

1

Tuesday, August 23rd 2005, 9:45pm

QTable with QTextEdit cells. How to reimplement functions?

I found some explanations in the docs about how to go about making a QTable without using QTableItems. The "Notes on large tables" bit. It gave me a lot of functions to reimplement. Some I understand, some I don't. I want the cells to be QTextEdit when editing, and QSimpleRichText when not editing.

Please tell me if I'm right about these functions on how I re-implement them for my table. The docs aren't clear enough.

paintCell(): Here I take row and col, use them to retrieve the richtext data from my datastructure and use QSimpleRichText to display it in the cell.

createEditor(): Here I create a QTextEdit widget and return it. I'm not clear on what initFromCell does, exactly, or why the docs tell me to create a QTableItem object, even though I won't use them.

setCellContentFromEditor(): Here I save the editor-content to my data-structure and delete the QTextEdit widget, so it won't take up more memory. What I don't understand is where I get the QTextEdit pointer address from. It's not a parameter, and I don't think it's a member I can access.

resizeData(): This will be an empty function. Or, well, I do something like 'len = 0;', so I don't get a compiler warning about not using a parameter. Incidentally, is there a better way to avoid a warning in a place like this?

item(), setItem(), takeItem(): These I don't understand at all. The first returns a QTableItem. The other two take one as an argument. How could I re-implement them to not use QTableItems? What do these functions really do, anyway? Will I (or my table) ever use them?

clearCell(): I suppose this deletes the entry in my data-structure of that cell, but I'm not positive.

insertWidget(), cellWidget(), clearCellWidget(): I'm not sure on these. Is the widget that these functions manipulate the QTextEdit? I don't think so. What do these functions do? Do I need them?

swapRows(), swapCells(), swapColumns(): Here I just manipulate the data in my data-structure accordingly, right? Nothing to do with painting anything. The table automatically repaints after these functions are called, right?

Even typing all these down here helped me understand some of it. :) But not everything. Please help me out a bit.

Thanks!

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

2

Tuesday, August 23rd 2005, 11:04pm

RE: QTable with QTextEdit cells. How to reimplement functions?

Quoted

Originally posted by Michiel
setCellContentFromEditor(): Here I save the editor-content to my data-structure and delete the QTextEdit widget, so it won't take up more memory. What I don't understand is where I get the QTextEdit pointer address from. It's not a parameter, and I don't think it's a member I can access.


QTable::cellWidget()

Quoted


Incidentally, is there a better way to avoid a warning in a place like this?


Use a nameless parameter, instead of void somefun(int x, int y) go with void somefun(int, int).

Quoted

item(), setItem(), takeItem(): These I don't understand at all. The first returns a QTableItem. The other two take one as an argument. How could I re-implement them to not use QTableItems? What do these functions really do, anyway? Will I (or my table) ever use them?


These are necessary if someone wants to access the cells. These are virtual, so as long as you don't mess with the table, you shouldn't need to change them (of course if your table item is derived from QTableItem, but I guess it is).

Quoted

clearCell(): I suppose this deletes the entry in my data-structure of that cell, but I'm not positive.

As far as I understand this function deletes an existing QTableItem and creates a new one in its place. You may want to reimplement it so that your own item gets created instead of a generic QTableItem.

Quoted

insertWidget(), cellWidget(), clearCellWidget(): I'm not sure on these. Is the widget that these functions manipulate the QTextEdit? I don't think so. What do these functions do? Do I need them?

Someone may want to insert a widget in the QTableItem's place, these functions implement that. Do you need them? Look at their sources, I'm sure you'll manage to come to an answer.

Quoted

swapRows(), swapCells(), swapColumns(): Here I just manipulate the data in my data-structure accordingly, right? Nothing to do with painting anything. The table automatically repaints after these functions are called, right?

The docs say they don't get repainted. One has to call updateContents manually, so you don't have to implement any painting here. If you inherit from QTableItem, I think you won't have to touch anything here.


I see you checked every function where the docs state that

Quoted

If you don't use QTableItems and want your users to be able to swap rows, e.g. for sorting, you will need to reimplement this function. (See the notes on large tables.)
.

If you want to use QSimpleRichText for display and QTextEdit for edition, then you'll be using QTableItems (you need to derive from it). The case where you wouldn't use a QTableItem is if you insert a widget into the table (like a push button). You probably use setCellWidget for that.

Michiel

Trainee

  • "Michiel" is male
  • "Michiel" started this thread

Posts: 112

Location: The Netherlands

Occupation: Student / Programmer

  • Send private message

3

Tuesday, August 23rd 2005, 11:11pm

So I DO need to use (or subclass from) QTableItem? Now I'm confused, because here I was thinking I wouldn't use it at all. What exactly needs to be derived from it? My data-structure or the widget used for edit? Because neither is possible, unless I use multiple inheritance.

This post has been edited 1 times, last edit by "Michiel" (Aug 23rd 2005, 11:11pm)


  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

4

Tuesday, August 23rd 2005, 11:44pm

Subclass QTableItem and modify its paint(...) member to use QSimpleRichText to render rich text and createEditor(...) to return a QTextEdit instead of QLineEdit.

Then you (1) either need to subclass QTable and modify some of the members you already mentioned to make this table use all your own items instead of QTableItems or (2) use QTable and just change the cells you want to have the special behaviour (using QTable::setItem).

I think that's about it.

Michiel

Trainee

  • "Michiel" is male
  • "Michiel" started this thread

Posts: 112

Location: The Netherlands

Occupation: Student / Programmer

  • Send private message

5

Wednesday, August 24th 2005, 11:17am

Sounds easy enough, but then the table won't use my own data-structure. I can quite easily map the cells onto my data structure, so I don't need a QTableItem inbetween. It would kind of get in the way.

Maybe I should reimplement it to not use its own private variables, but write directly to the data-structure. Functions setText() and text(), I suppose. And I should reimplement setContentFromEditor(), setWordWrap() and wordWrap().

Hm.. I'll go try this and post the results. Might be useful for someone else.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

6

Wednesday, August 24th 2005, 1:04pm

The data structure may reside inside your QTableItem subclass.

Michiel

Trainee

  • "Michiel" is male
  • "Michiel" started this thread

Posts: 112

Location: The Netherlands

Occupation: Student / Programmer

  • Send private message

7

Wednesday, August 24th 2005, 1:06pm

Then I'll have to use multiple inheritance, or just give the QTableItem a pointer. I prefer the second.

  • "wysota" is male

Posts: 4,276

Location: Warsaw, POLAND

  • Send private message

8

Wednesday, August 24th 2005, 2:23pm

Quoted

Originally posted by Michiel
Then I'll have to use multiple inheritance, or just give the QTableItem a pointer. I prefer the second.


Source code

1
2
3
4
class MyTableItem : public QTableItem {
private:
  my_structure data;
};


or multiple inheritance as already mentioned. Both approaches are ok.

Michiel

Trainee

  • "Michiel" is male
  • "Michiel" started this thread

Posts: 112

Location: The Netherlands

Occupation: Student / Programmer

  • Send private message

9

Saturday, August 27th 2005, 9:39pm

Do you have any idea what kind of functions I must re-implement in QTable to tell it to use LMTableItems instead of QTableItems? The only functions I found that say "new QTableItem" someplace are setText() and setPixmap(). I subclassed both and tried them out. The new table doesn't use QTextEdit widgets. I think I'll blame it on my subclass of QTable, since it should obviously reimplement more functions than just those two. But in the source, I can't find any other place where they create new table-items.

Some help, please?

Thanks!

10

Sunday, August 28th 2005, 8:28am

Quoted

Originally posted by Michiel
Do you have any idea what kind of functions I must re-implement in QTable to tell it to use LMTableItems instead of QTableItems?


AFAIK none. After setting the size of QTable you call setItem(row,col, new MyCell()) for each item.

;-) Currently I am doing the same thing -- writing my QTable and QTableItem versions just to get EditGrid funcionality. Till now I didn't solved why I can use only OnTyping mode and how to make selections with mouse work.

have a nice day
bye

Michiel

Trainee

  • "Michiel" is male
  • "Michiel" started this thread

Posts: 112

Location: The Netherlands

Occupation: Student / Programmer

  • Send private message

11

Sunday, August 28th 2005, 9:53pm

Thanks! That, indeed, seems to be the best way to handle it. Just use the setItem function on every single cell that is created.

I have another related question. I would like the cells to have OnTyping edit-mode, but when I type when a cell is selected (but not showing an editor yet), my own edit-widget (QTextEdit) doesn't appear. A normal QLineEdit appears, and when I press enter (or the cell loses focus another way) a segfault appears.

How can I use a QTextEdit for this?

12

Monday, August 29th 2005, 7:59am

Quoted

Originally posted by Michiel
Thanks! That, indeed, seems to be the best way to handle it. Just use the setItem function on every single cell that is created.


Yup. Not very efficient however.

Quoted


I have another related question. I would like the cells to have OnTyping edit-mode, but when I type when a cell is selected (but not showing an editor yet), my own edit-widget (QTextEdit) doesn't appear.
A normal QLineEdit appears,


Did you override createEditor in QTable? You create QTextEdit there and return pointer to it. I see forthcoming your next question so here is the answer :-) -- the arrow keys don't work because you have to override /thank you Qt/ eventFilter for arrow keys /or you have to remove filter/ OR eventFilter and event for arrow keys + tab.

have a nice day
bye