见下面程序:
// Tchecklistitem.cpp: implementation of the Tchecklistitem class.
//
//////////////////////////////////////////////////////////////////////
#include<qpen.h>
#include<qrect.h>
#include <qpainter.h>
#include "Tchecklistitem.h"
#include <qlistview.h>
class QListView;
class QCheckListItem;
class QPainter;
class QColorGroup;
class QRect;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Tchecklistitem::Tchecklistitem( QListView * parent, const QString & text) :QCheckListItem( QListView * parent)
{
m_text = text;
}
Tchecklistitem::Tchecklistitem(QCheckListItem *parent , const QString & text ):QCheckListItem(QCheckListItem *parent )
{
m_text = text;
}
Tchecklistitem::~Tchecklistitem()
{
}
void Tchecklistitem::paintFocus ( QPainter * p, const QColorGroup & cg, const QRect & r )
{
QPen pen = p->pen();
pen.setWidth( 2 );
p->setPen( pen );
p->setBrush(Qt::red);
// QRect r = rect();
// r.addCoords( 10, 10, -10, -10 );
p->drawRect ( r );
}
// Tchecklistitem.h: interface for the Tchecklistitem class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_TCHECKLISTITEM_H__86576F20_EADD_4781_83B8_38087B289F73__INCLUDED_)
#define AFX_TCHECKLISTITEM_H__86576F20_EADD_4781_83B8_38087B289F73__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <qlistview.h>
#include <qstring.h>
#include <qpixmap.h>
#include <qstyle.h>
class QListView;
class QCheckListItem;
class QPainter;
class QColorGroup;
class QRect;
class Tchecklistitem : public QCheckListItem
{
public:
Tchecklistitem(QListView * parent, const QString & text);
Tchecklistitem(QCheckListItem * parent, const QString & text);
virtual ~Tchecklistitem();
virtual void paintFocus ( QPainter * p, const QColorGroup & cg, const QRect & r ) ;
private:
QString m_text;
};
#endif // !defined(AFX_TCHECKLISTITEM_H__86576F20_EADD_4781_83B8_38087B289F73__INCLUDED_)
/****************************************************************************
** $Id: qt/checklists.h 3.3.4 edited May 27 2003 $
**
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#ifndef CHECKLISTS_H
#define CHECKLISTS_H
#include <qwidget.h>
class QListView;
class QLabel;
class CheckLists : public QWidget
{
Q_OBJECT
public:
CheckLists( QWidget *parent = 0, const char *name = 0 );
protected:
QListView *lv1, *lv2;
QLabel *label;
protected slots:
void copy1to2();
void copy2to3();
};
#endif
/****************************************************************************
** $Id: qt/checklists.cpp 3.3.4 edited Sep 10 2003 $
**
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "checklists.h"
#include "Tchecklistitem.h"
#include <qlistview.h>
#include <qvbox.h>
#include <qlabel.h>
#include <qvaluelist.h>
#include <qstring.h>
#include <qpushbutton.h>
#include <qlayout.h>
/*
* Constructor
*
* Create all child widgets of the CheckList Widget
*/
CheckLists::CheckLists( QWidget *parent, const char *name )
: QWidget( parent, name )
{
QHBoxLayout *lay = new QHBoxLayout( this );
lay->setMargin( 5 );
// create a widget which layouts its childs in a column
QVBoxLayout *vbox1 = new QVBoxLayout( lay );
vbox1->setMargin( 5 );
// First child: a Label
vbox1->addWidget( new QLabel( "Check some items!", this ) );
// Second child: the ListView
lv1 = new QListView( this );
vbox1->addWidget( lv1 );
lv1->addColumn( "Items" );
lv1->setRootIsDecorated( TRUE );
// create a list with 4 ListViewItems which will be parent items of other ListViewItems
QValueList<QListViewItem *> parentList;
parentList.append( new QCheckListItem( lv1, "Parent Item 1", QCheckListItem::CheckBoxController ) );
parentList.append( new QCheckListItem( lv1, "Parent Item 2", QCheckListItem::CheckBoxController ) );
parentList.append( new QCheckListItem( lv1, "Parent Item 3", QCheckListItem::CheckBoxController ) );
parentList.append( new QCheckListItem( lv1, "Parent Item 4", QCheckListItem::CheckBoxController ) );
QListViewItem *item = 0;
unsigned int num = 1;
// go through the list of parent items...
for ( QValueList<QListViewItem*>::Iterator it = parentList.begin(); it != parentList.end();
( *it )->setOpen( TRUE ), ++it, num++ ) {
item = *it;
// ...and create 5 checkable child ListViewItems for each parent item
for ( unsigned int i = 1; i <= 5; i++ )
(void)new Tchecklistitem( item, QString( "%1. Child of Parent %2" ).arg( i ).arg( num ) );
}
// Create another widget for layouting
QVBoxLayout *tmp = new QVBoxLayout( lay );
tmp->setMargin( 5 );
// create a pushbutton
QPushButton *copy1 = new QPushButton( " -> ", this );
tmp->addWidget( copy1 );
copy1->setMaximumWidth( copy1->sizeHint().width() );
// connect the SIGNAL clicked() of the pushbutton with the SLOT copy1to2()
connect( copy1, SIGNAL( clicked() ), this, SLOT( copy1to2() ) );
// another widget for layouting
QVBoxLayout *vbox2 = new QVBoxLayout( lay );
vbox2->setMargin( 5 );
// and another label
vbox2->addWidget( new QLabel( "Check one item!", this ) );
// create the second listview
lv2 = new QListView( this );
vbox2->addWidget( lv2 );
lv2->addColumn( "Items" );
lv2->setRootIsDecorated( TRUE );
// another widget needed for layouting only
tmp = new QVBoxLayout( lay );
tmp->setMargin( 5 );
// create another pushbutton...
QPushButton *copy2 = new QPushButton( " -> ", this );
lay->addWidget( copy2 );
copy2->setMaximumWidth( copy2->sizeHint().width() );
// ...and connect its clicked() SIGNAL to the copy2to3() SLOT
connect( copy2, SIGNAL( clicked() ), this, SLOT( copy2to3() ) );
tmp = new QVBoxLayout( lay );
tmp->setMargin( 5 );
// and create a label which will be at the right of the window
label = new QLabel( "No Item yet...", this );
tmp->addWidget( label );
}
/*
* SLOT copy1to2()
*
* Copies all checked ListViewItems from the first ListView to
* the second one, and inserts them as Radio-ListViewItem.
*/
void CheckLists::copy1to2()
{
// create an iterator which operates on the first ListView
QListViewItemIterator it( lv1 );
lv2->clear();
// Insert first a controller Item into the second ListView. Always if Radio-ListViewItems
// are inserted into a Listview, the parent item of these MUST be a controller Item!
QCheckListItem *item = new QCheckListItem( lv2, "Controller", QCheckListItem::Controller );
item->setOpen( TRUE );
// iterate through the first ListView...
for ( ; it.current(); ++it )
// ...check state of childs, and...
if ( it.current()->parent() )
// ...if the item is checked...
if ( ( (QCheckListItem*)it.current() )->isOn() )
// ...insert a Radio-ListViewItem with the same text into the second ListView
(void)new QCheckListItem( item, it.current()->text( 0 ), QCheckListItem::RadioButton );
if ( item->firstChild() )
( ( QCheckListItem* )item->firstChild() )->setOn( TRUE );
}
/*
* SLOT copy2to3()
*
* Copies the checked item of the second ListView into the
* Label at the right.
*/
void CheckLists::copy2to3()
{
// create an iterator which operates on the second ListView
QListViewItemIterator it( lv2 );
label->setText( "No Item checked" );
// iterate through the second ListView...
for ( ; it.current(); ++it )
// ...check state of childs, and...
if ( it.current()->parent() )
// ...if the item is checked...
if ( ( (QCheckListItem*)it.current() )->isOn() )
// ...set the text of the item to the label
label->setText( it.current()->text( 0 ) );
}
/****************************************************************************
** $Id: qt/main.cpp 3.3.4 edited May 27 2003 $
**
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "checklists.h"
#include <qapplication.h>
int main( int argc, char **argv )
{
QApplication a( argc, argv );
CheckLists checklists;
checklists.resize( 650, 350 );
checklists.setCaption( "Qt Example - CheckLists" );
a.setMainWidget( &checklists );
checklists.show();
return a.exec();
}
上述程序编译总报错:
--------------------Configuration: checklists - Win32 Debug--------------------
Compiling...
checklists.cpp
D:\工具箱\编程\checklist\checklists.cpp(63) : error C2664: '__thiscall Tchecklistitem::Tchecklistitem(class QListView *,const class QString &)' : cannot convert parameter 1 from 'class QListViewItem *' to 'class QListView *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Tchecklistitem.cpp
D:\工具箱\编程\checklist\Tchecklistitem.cpp(21) : error C2275: 'QListView' : illegal use of this type as an expression
C:\Qt\3.3.4\include\qlistview.h(233) : see declaration of 'QListView'
D:\工具箱\编程\checklist\Tchecklistitem.cpp(26) : error C2275: 'Tchecklistitem::QCheckListItem' : illegal use of this type as an expression
Generating Code...
Error executing cl.exe.
checklists.exe - 3 error(s), 0 warning(s)
请教那位大侠,看看到底错误在那里呀?