Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

Qt (B2) Input Multi-line with QTableWidget

Published on| June 18th, 2009 | No Comment.
QStyledItemDelegate to use inheritance

This class will be entrusted the key control. QStyledItemDelegate is used here, QTableWidget that can also be used, QTreeWidget can also be used. The same class is available.

So, QStyledItemDelegate to inherit, let's create a class.


[multidelegate.h]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <QStyledItemDelegate>
 
class MultiDelegate : public QStyledItemDelegate
{
	Q_OBJECT
 
public:
	MultiDelegate(QWidget *parent = 0);
 
	virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
	                      const QModelIndex &index) const;
	virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
	virtual void setModelData(QWidget *editor, QAbstractItemModel *model,
	                  const QModelIndex &index) const;
}

[multidelegate.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <QtGui>
 
#include "MultiDelegate.h"
 
MultiDelegate::MultiDelegate(QWidget *parent) 
	: QStyledItemDelegate(parent) 
{
}
 
QWidget *MultiDelegate::createEditor(QWidget *parent,
                                    const QStyleOptionViewItem & /*option*/,
                                    const QModelIndex &/*index*/) const
 
{
	QPlainTextEdit *editor = new QPlainTextEdit(parent);
	return editor;
}
 
void MultiDelegate::setEditorData(QWidget *editor,
                                 const QModelIndex &index) const
{
	QPlainTextEdit *mEditor = qobject_cast<QPlainTextEdit *>(editor);
	mEditor->setPlainText(index.data().toString());
}
 
void MultiDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                const QModelIndex &index) const
{
	QPlainTextEdit *mEditor = qobject_cast<QPlainTextEdit *>(editor);
	model->setData(index, mEditor->toPlainText());
}

This is, to override a virtual function, respectively, QPlainTextEdit and control.

  • createEditor
    - This method is called when the input state, passed by this method appears as a QWidget as a screen.
  • setEditorData
    - This method is called when you enter the initial configuration screen. Here, we present QTableWidget initializes the information of the cell.
  • setModelData
    - This method is called when you have finished the editing screen. Here, the current QTableWidget input and output information to the cells.

So, main.cpp See the changes sought.

[main.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <QtGui>
#include "MultiDelegate.h"
 
 
int main(int argc, char *argv[])
{
	QApplication app(argc, argv);
 
	QTableWidget tableWidget(3, 3);
	tableWidget.setItemDelegate(new MultiDelegate);
	//	set edit trigger event
	tableWidget.setEditTriggers(QAbstractItemView::DoubleClicked
	                            | QAbstractItemView::SelectedClicked
								| QAbstractItemView::EditKeyPressed );
 
	//	set title
	QStringList headerLabels;
	headerLabels << "Name" << "Attribute" << "Comment";
	tableWidget.setHorizontalHeaderLabels(headerLabels);
 
	QTableWidgetItem *item[3];
	//	set sample data values.
	for (int nrow = 0; nrow<3; nrow++ ) {
		item[0] = new QTableWidgetItem(QString("sample item name #%1").arg(nrow+1));
		item[1] = new QTableWidgetItem(QString("att #%1").arg(nrow+1));
		item[2] = new QTableWidgetItem(QString("comment #%1\nreturn ").arg(nrow+1));
 
		for(int ncol=0;ncol<3;ncol++){
			tableWidget.setItem(nrow, ncol, item[ncol]);
 
		}
	}
 
	//	fit of column size at contents.
	tableWidget.resizeColumnsToContents();
	tableWidget.resize(500, 300);
 
	//	table show.
	tableWidget.show();
 
	return app.exec();
}

Line 10, setItemDelegate in, Delegate only are associated with the class.

And compile it and run, you'll see a screen like the following.



The work, double-click the mouse, the input state, the screen is operating as expected.
And actually try the action, unfortunately, "Enter" will close the entry screen and enter.

Using QTextEdit as QPlainTextEdit, "Enter" to enter the input screen does not close.

If you override 'eventFilter' method, this window will not close when you input 'Enter' key.
For example, as the following.

[multidelegate.cpp]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
bool MultiDelegate::eventFilter ( QObject * editor, QEvent * oevent )
{
	QPlainTextEdit *mEditor = qobject_cast<QPlainTextEdit *>(editor);
	if (!mEditor || !oevent)
		return false;
 
 
	int nevent_type=oevent->type();
	if (
		nevent_type== QEvent::KeyPress ||
		nevent_type== QEvent::KeyRelease
		) {
		QKeyEvent *keyevt=static_cast<QKeyEvent *>(oevent);
		if(keyevt){
			switch(keyevt->key()){
			case Qt::Key_Enter:
			case Qt::Key_Return:
				if(nevent_type==QEvent::KeyRelease){
					return true;
				}
				mEditor->insertPlainText("\n");
				return true;
				break;
			default:
				break;
			}
		}
	}
	return QStyledItemDelegate::eventFilter( editor,oevent );
}

eventFilter came into the event, the key event, "Enter" key inputs cases, QPlainTextEdit to add information to the new line and end the process.

Return to the true value is specified, where it means that the processing event, this event h, will be destroyed.

And compile it and run, you will also enter閉JINAKU enter a new line.


This time, QTableWidget we customize the type of treatment. In addition to the above EXCEL can also Ctrl + Enter to start a new line. (Included in the sample source)

Also, QPlainTextEdit QComboBox can also be used to replace. (Basically, if QWidget derived class, you can use in their own class) QTableWidget is a good class to use a variety of applications and would be spread.

This way, often to read the document, it is not surprising. But who is to skim through the manual as I was, surprisingly, you may get addicted.

If you have noticed something, I would appreciate comments.

Comments

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ