////.h#ifndef VALIDATORTABLEITEM_H#define VALIDATORTABLEITEM_H#include <qtable.h>#include <qvalidator.h>class ValidatorTableItem : public QTableItem{public: ValidatorTableItem(QTable * table, EditType et, const QString & text, QRegExpValidator * V = 0); ValidatorTableItem(QTable * table, EditType et, const QString & text, const QPixmap & p, QRegExpValidator * = 0); virtual QWidget * createEditor() const; QRegExpValidator * Valdtr; // We allow the direct, public manipulation of Valdtr to keep the object as small as possible rather than adding the size of a setValidator function};#endif////////////////////////////////////////////cpp#include "validatortableitem.h"#include <qlineedit.h>ValidatorTableItem::ValidatorTableItem(QTable * table, EditType et, const QString & text, QRegExpValidator * V) : QTableItem(table, et, text){ Valdtr = V; setReplaceable(false); if ( Valdtr && (! text.isEmpty()) && text.find(Valdtr->regExp()) == -1 ) { // i.e., we have not passed valid text in the constructor setText(""); }}ValidatorTableItem::ValidatorTableItem(QTable * table, EditType et, const QString & text, const QPixmap & p, QRegExpValidator * V) : QTableItem(table, et, text, p){ Valdtr = V; setReplaceable(false); if ( Valdtr && (! text.isEmpty()) && text.find(Valdtr->regExp()) == -1 ) { // i.e., we have not passed valid text in the constructor setText(""); }}QWidget * ValidatorTableItem::createEditor() const{ QLineEdit *e = new QLineEdit( table()->viewport(), "qt_tableeditor" ); e->setFrame( FALSE ); if ( Valdtr ) { e->setValidator(Valdtr); } e->setText( text() ); return e;}/////////////////Примерно такой у меня будет мейн.//main#include <qapplication.h>#include <form4.h>#include <stdio.h>#include <qpushbutton.h>#include <qvalidator.h>#include <qlineedit.h>#include <qstring.h>#include <qtable.h>#include <qlabel.h>#include <validatortableitem.h>int main( int argc, char ** argv ){ QApplication a( argc, argv ); Form4 *w = new Form4(); w->show(); a.connect( &a, SIGNAL( lastWindowClosed() ),&a, SLOT( quit() )); a.connect( w->quit, SIGNAL(clicked()),&a, SLOT( quit() ) ); a.connect( w->push, SIGNAL(clicked()), w, SLOT(slot_simplex())); QDoubleValidator * val = new QDoubleValidator( w ); w->te->setValidator(val); w->tbl->horizontalHeader()->setLabel(0, "Name");QString string0;QRegExp rx( "-?\\d{1,3}" );QRegExpValidator *vldtr= new QRegExpValidator( rx, w );////ValidatorTableItem *vti;////w->tbl->setItem( 2, 2, ValidatorTableItem::QTableItem(w->tbl,QTableItem::Always,string0,vldtr));w->tbl->setItem(1 , 1,new QTableItem( w->tbl, QTableItem::WhenCurrent, QString::number( 1 * 1 ) ) ); return a.exec();} [size=9px][color=#999999]добавлено спустя 1 минуту:[/color][/size] /* validatortableitem.h defines a QTableItem which allows a QRegExpValidator to be set *//* Copyright (C) 2003 John A. Sullivan III This program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.e-mail: john.sullivan@nexusmgmt.com*//* Revision 0 - December 31, 2003 - John Sullivan */
#ifndef __TVALIDATORTABLEITEM_H__INCL#define __TVALIDATORTABLEITEM_H__INCL#include <qtable.h> class QValidator;class QLineEdit;class TValidatorTableItem : public QTableItem { public: TValidatorTableItem(QTable* table, EditType et ); TValidatorTableItem(QTable* table, EditType et, QValidator* pValidator); TValidatorTableItem(QTable* table, EditType et, const QString& text ); TValidatorTableItem(QTable* table, EditType et, const QString& text, QValidator* pValidator); virtual ~TValidatorTableItem(); virtual QWidget * createEditor() const; QValidator* validator()const { return m_pValidator; }; void setValidator(QValidator* pValidator);private: QValidator* m_pValidator; QLineEdit* m_pEdit;}; #endif
#include <qvalidator.h>#include <qlineedit.h> #include "TValidatorTableItem.h" TValidatorTableItem::TValidatorTableItem(QTable* table, EditType et): QTableItem(table, et), m_pValidator(NULL), m_pEdit(NULL){}TValidatorTableItem::TValidatorTableItem(QTable* table, EditType et, QValidator* pValidator): QTableItem(table, et), m_pValidator(NULL), m_pEdit(NULL){ setValidator(pValidator);}TValidatorTableItem::TValidatorTableItem(QTable* table, EditType et, const QString& text): QTableItem(table, et, text), m_pValidator(NULL), m_pEdit(NULL){}TValidatorTableItem::TValidatorTableItem(QTable* table, EditType et, const QString& text, QValidator* pValidator): QTableItem(table, et, text), m_pValidator(NULL), m_pEdit(NULL){ setValidator(pValidator);}void TValidatorTableItem::setValidator(QValidator* pValidator){ if (NULL != m_pValidator) delete m_pValidator; m_pValidator = pValidator; if (NULL != m_pEdit) m_pEdit->setValidator(m_pValidator); QString content = text(); if (NULL != m_pValidator) { int iPos = 0; if (QValidator::Invalid == m_pValidator->validate(content, iPos)) setText(""); }}QWidget* TValidatorTableItem::createEditor() const{ TValidatorTableItem* pItem = const_cast<TValidatorTableItem*>(this); pItem->m_pEdit = dynamic_cast<QLineEdit*>(QTableItem::createEditor()); if (NULL != m_pEdit) { if (NULL != m_pValidator) m_pEdit->setValidator(m_pValidator); } return m_pEdit;}TValidatorTableItem::~TValidatorTableItem(){ setValidator(NULL);}
#include <qapplication.h>#include <qvalidator.h>#include "TValidatorTableItem.h"int main(int argc, char* argv[]){ QApplication app(argc, argv, true); QTable myTable; myTable.show(); myTable.setNumCols(4); myTable.setNumRows(16); //==============Variant number 1================= //TValidatorTableItem* pItem = new TValidatorTableItem(&myTable, QTableItem::Always); //pItem->setValidator(new QIntValidator(1, 100, NULL)); //==============Variant number 2================= //myTable.setItem(1, 1, new TValidatorTableItem(&myTable, QTableItem::Always, new QIntValidator(1, 100, NULL))); //==============Variant number 3================= TValidatorTableItem* pItem = new TValidatorTableItem(&myTable, QTableItem::Always, new QIntValidator(1, 100, NULL)); myTable.setItem(1, 1, pItem); app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit())); return app.exec();}