PDA

View Full Version : How to subclass a control in *.ui?



coralbird
4th April 2006, 10:05
For I use layout to arrange the control's position.But i have to inherit some control,for example, qtable,to realize some function.How Can I reach this? Thanks!!

jrideout
4th April 2006, 14:36
Well, I'm not sure this is what you mean, but...
If you have a class that is derived from a widget available in designer like "class myCustomTableView : public QTableView" then you can right click the widget in designer and choose promote to custom widget. You must specify the proper header file for your custom widget.

coralbird
5th April 2006, 03:12
Well, I'm not sure this is what you mean, but...
If you have a class that is derived from a widget available in designer like "class myCustomTableView : public QTableView" then you can right click the widget in designer and choose promote to custom widget. You must specify the proper header file for your custom widget.

What I mean is that ,for example,if there is a QTable in *.ui, and I have a class named
QMyTable derived from QTable,How can i relate these two?In Mfc,I can use CWnd::SubclassDlgItem to do this. this is the detailed description in MSDN.

And I do my best to understand and test what you descripted,but I have no idea at last:

right click the widget in designer and choose promote to custom widget. ,there is no such option in right click options.


CWnd::SubclassDlgItem
This method dynamically subclasses a control created from a dialog box template, and attach it to this CWnd object. When a control is dynamically subclassed, windows messages will route through the CWnd message map and call message handlers in the CWnd class first. Messages that are passed to the base class will be passed to the default message handler in the control.

This method attaches the Windows control to a CWnd object and replaces the WndProc and AfxWndProc functions of the control. The function stores the old WndProc in the location returned by the CWnd::GetSuperWndProcAddr method.

BOOL SubclassDlgItem(
UINT nID,
CWnd* pParent );
Parameters
nID
Specifies the control ID.
pParent
Specifies the control parent, this is usually a dialog box.
Return Value
Nonzero if the function is successful; otherwise, it is zero.

Example
class CMyButton : public CButton {...};
// m_myButton is a CMyButton object member of CAboutDlg

BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// IDC_BUTTON1 is the ID for a button on the
// dialog template used for CAboutDlg.
m_myButton.SubclassDlgItem(IDC_BUTTON1, this);

return TRUE; // Return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

jrideout
5th April 2006, 07:58
Sorry my solution is for Qt4, I just noticed you are using Qt3.

Anyway for folks that read this thread and are using 4 here is a clarification.

In designer, with your form open, right click the widget in the form.

The context menu from right clicking should look like this:


Change objectName
Change toolTip
Change whatsThis
------------------
Promote to Custom Widget
------------------
Cut
Copy
...


Select "Promote to Custom Widget". A dialog will appear where you can put in the name of the derived class and a header that defines it.

You can edit the ui manually to achive the same effect. Try something like this:


<ui version="4.0" >
<class>Dialog</class>
<widget class="QDialog" name="Dialog" >
<property name="windowTitle" >
<string>Dialog</string>
</property>
<widget class="QTableView" name="tableView" >
</widget>
<widget class="myCustomTable" name="customTableView" >
</widget>
</widget>
<customwidgets>
<customwidget>
<class>myCustomTable</class>
<extends>QTableView</extends>
<header>mycustomtable.h</header>
<container>0</container>
<pixmap></pixmap>
</customwidget>
</customwidgets>
</ui>

jrideout
5th April 2006, 08:03
For qt3 try tools->custom->Edit Custom Widget in designer

coralbird
5th April 2006, 14:40
First special thanks to jrideout!


For qt3 try tools->custom->Edit Custom Widget in designer

I have find the entrance,and I will test tomorrow. Thanks again.:D

coralbird
6th April 2006, 03:33
For qt3 try tools->custom->Edit Custom Widget in designer

Sure,Through this function,I have reached my target.It's really a usful function.
thanks jrideout again.