PDA

View Full Version : QTable - paintCell() - how to do with it?



mp33919
25th March 2009, 20:05
Hi, hopefully, someone can help me out about the "paintCell()" issues.

All I wanted to do is paint a few selected cells in a relatively large table.

I have done all the seraches in the internet. All I saw were "re-implement" the paintCell(). OK I did it.

1. I created a form - mainForm and drag a table into the form.
2. So, in my project, I have mainform.ui mainform.ui.h and main.cpp
3. So, how can I re-implement paintCell()? I create a class coded in the mainform.ui.h named Table which is inheriated from QTable. And re-write the paintCell() function, sounds right?
4. Then, I pass the compiler. But, I did not pass the linker. The error message is: undefined refernce to mainform::paintCell(... ...) So, what went wrong?

Then, I am thinking, I can pass the linker, how I can invoke this paintCell()? My table was dragged into the form and its the object of the QTable.

I will be really appreciated if anyone can give me a clue.

Best Regards,

Here is the structure of my code:

// filename: mainform.ui.h


class Table::public QTable
{
Q_OBJECT
public:
void paintCell(QPainter *painter, int row, in col, const QRect &rc, bool selected, const QColorGroup &cg);
};

void Table::paintCell( ... ... )
{
... ...
}

// mainform code starts here

void mainform::init()
{
... ...
}

wysota
25th March 2009, 23:29
You don't call paintCell(). It's a virtual method of the table that gets called by the table itself. You have to use your subclass of QTable instead of QTable in your form, for example using the custom widgets mechanism available in Designer's menu. You need to place an instance of "Table" on the form and C++ will pick up your implementation of paintCell().

mp33919
26th March 2009, 13:20
Hi,

I understand that I should use the Table object instead of QTable object. But the "problem" is "how"? When I dragged the table (i named it as colorTable) into the form, the colorTable is the object of QTable not the Table. I even tried to modify the mainform.ui, which is an html file. But as you know, my mainform can no longer be loaded.

The only other way I could think of is to use the pointer. Should I use the Table object pointer to point to the colorTable? Will it work?

by the way, the reason of the error during linking is because I coded the Table class inside the mainform.ui.h. After I posted the questions, I separated the Table class into table.h and table.cpp than the project can be compiled and the executable code was generated.

talk2amulya
26th March 2009, 15:59
when u drag the QTable..right click on it and choose "Promote to" option..there u can mention your custom table class.

wysota
26th March 2009, 17:07
When I dragged the table (i named it as colorTable) into the form, the colorTable is the object of QTable not the Table.

Did you use the Tools > Edit Custom Widgets mechanism to fake a new widget type? You should fill a dialog that pops up and if you do that correctly, there will be a new widget available in the "Custom Widgets" section of the widget toolbox.

mp33919
14th April 2009, 18:00
Hi, sorry, I have gone for other things for a while ...
I am not sure what you mean about "fake a new widget type"
I tried to use Tools > Edit Custom Widgets and I got a "green little Qt icon" and that "green little Qt icon" now is stay in my Custom Widgets section "permanently", I could not get rid of that "green little Qt icon".

Anyway, let me understand two of your previous messages first:
1. I have to create a custom widget maybe named Table
2. Then, drag this Table widget into my mainform instead of QTable
3. Since the paintCell is re-implemented in the Table widget, the Qt will pick it up automatically

If that is correct, before I do it, may I ask you why we have to go such a lot of trouble just to paint the QTable's cells?

Another question I have to ask, if I ever want to modify the Table custom widget, will the new modification affect every program I have used that Table custom widget. (It seems to me this "custom widget" concept is whole like Windows' Active X control.)

Thanks for your reply. I am looking forward to read your reply.

wysota
14th April 2009, 21:57
Hi, sorry, I have gone for other things for a while ...
I am not sure what you mean about "fake a new widget type"
I tried to use Tools > Edit Custom Widgets and I got a "green little Qt icon" and that "green little Qt icon" now is stay in my Custom Widgets section "permanently", I could not get rid of that "green little Qt icon".
That's fine. It's the fake widget I was talking about. If you filled the data correctly in the preceding dialog, after compilation the "green little Qt icon" will be replaced by a real widget according to what you entered in the dialog.


Anyway, let me understand two of your previous messages first:
1. I have to create a custom widget maybe named Table
2. Then, drag this Table widget into my mainform instead of QTable
3. Since the paintCell is re-implemented in the Table widget, the Qt will pick it up automatically
That's correct.


If that is correct, before I do it, may I ask you why we have to go such a lot of trouble just to paint the QTable's cells?
You don't have to. You can upgrade to Qt4 and do it in a much easier way. Qt3 is a 10 year old technology, remember that.


Another question I have to ask, if I ever want to modify the Table custom widget, will the new modification affect every program I have used that Table custom widget. (It seems to me this "custom widget" concept is whole like Windows' Active X control.)
No, it's not like ActiveX. Custom widgets are real classes that are compiled into your application as any other class and any other widget already available in Qt. The whole thing about "custom widgets" and "little green Qt icons" is that Designer doesn't know anything about your class. To be able to generate a correct source code that is later compiled it needs to know the class name, include file name, base class and maybe some other details about your class. To provide that information you can go using one of two ways.

One is to implement a full-blown plugin for Designer which means writing code for yet another class that is then compiled as a library and dynamically loaded into Designer so that you can use the widget as any other already existing widget. But this requires some coding and is prone to errors so if you don't need full control over the widget from within Designer, you can use the second approach - just provide appropriate information about your class to Designer and it will create a fake widget representation (the green icon thingy) for you which you can place on Designer and manipulate like any other widget but it won't have properties (and looks) specific to your widget until you compile the application and the fake widget gets replaced by a real one.

So to answer your question, if you modify your class, it won't affect applications that are already compiled. You'll have to rebuild them using the updated class to see the changes. There is an exception to this rule - if you compile your widget as a separate dynamic library (dll) and you link against the dll instead of directly embedding code for the class inside your application, updating the library will update all applications that link against it.

ActiveX controls are loaded dynamically into your application while the application is running. Here the class is compiled into your application like any other class or is dynamically linked to it like any other library (like Qt itself).

mp33919
21st April 2009, 16:51
Hi,
Thanks for the help you gave to me.
It sounded very logical to upgrade the Qt3.3.x to Qt4 so that I don't need to go through all the "troubles" just to paint a single cell of my QTable. I did, last few days I have my Linux Qt3.3.6 upgraded to Qt4.5. But now I got "serious" problem ... ...
I can not "re-build" my existed project with new qtcreator (that is Qt4's IDE, right?). I did not know what to do with it.
Then, I tried to open the one of my ui file. I got the error message as: "unable to launch /home/berlin/dev/qt-45/bin/uic3"
and it also told me: "Please run it through uic3 -convert to convert it to Qt-4's ui format."
I am not sure I am on the "right track" or not. I mean, is this a normal step to convert Qt3 designer project into Qt4 qtcreator project? Should I need to convert every ui file one by one or there is one single easy step to convert entired .pro to Qt4? And, is there a better "Qt4 assistant" than the Qt Creator Manual which tell me almost nothing.

Thanks again.

wysota
23rd April 2009, 19:55
You have to port your project from Qt3 to Qt4. It requires some effort but it is worth it. Read the section about porting in your Qt Assistant.