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 | 3 Comments.
Description:
The theme was that multi-line text is able to input with QTableWidget. QTableWidget So, by default, QLineEdit for input is available as a Widget. QTableWidget can type in the state, and double-click the mouse to create, you can enter.
This time, the input and QTextEdit QPlainTextEdit Widget can enter multiple lines and I would like to describe how to switch.

Sample source code you used here:

A simple screen QTableWidget
Let's make a simple screen QTableWidget sample.
The following code, 3x3 QTableWidget easy to create a screen.

[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
#include <QtGui>
 
 
int main(int argc, char *argv[])
{
	QApplication app(argc, argv);
 
	QTableWidget tableWidget(3, 3);
	//	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 from line 12, defines the behavior when the input state.
Here, double-click, select a state and click the enter key and F2 in operation, the transition to the state input.
Line 33 is directed to both the horizontal and vertical scaling with the high content of the cell.

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



The third column, the information included in the information line, QLineEdit, it is ignored.


, To be able to type this, from the document, find immediately, QTableWidget the void setCellWidget () I think.

So, if you use this method, or Let's look at it?

setCellWidget use, create a multi-line screen
Contact QTableWidget simple try to modify the sample screen.
The following code, 3x3 QTableWidget easy to create a screen.

[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
43
#include <QtGui>
 
 
int main(int argc, char *argv[])
{
	QApplication app(argc, argv);
 
	QTableWidget tableWidget(3, 3);
	//	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]);
 
			QPlainTextEdit *ptext=new QPlainTextEdit(&tableWidget);
			ptext->setPlainText(item[ncol]->text());
			tableWidget.setCellWidget(nrow, ncol, ptext);
		}
	}
 
	//	fit of column size at contents.
	tableWidget.resizeColumnsToContents();
	tableWidget.resize(500, 300);
 
	//	table show.
	tableWidget.show();
 
	return app.exec();
}

It's easy.
Line 29 from line 31, QPlainTextEdit the setCellWidget and associations in each cell.

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



All the cells, the state can enter multiple lines of text at all times, perhaps, different from the screen and I had hoped. This is, QGridLayout in QPlainTextEdit screen is a screen similar to the paste.

In a similar operation in the first sample (to switch the input state) operation, the QLineEdit QPlainTextEdit simply to replace, should I?

Using the Delegate class is inherited, but, QStyledItemDelegate to create an inherited class, QTableWidget let's control.

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

3 Comments. “Qt (B2) Input Multi-line with QTableWidget”


  1. 通りすがり
    April 28th, 2019 @ 18:48:24

    33行目は、セルの多いさを内容にあわせて縦横ともに拡大・縮小するように指示しています。
    -> 大きさ

  2. 通りすがり
    April 28th, 2019 @ 18:53:46

    2頁目は同じ内容に見受けられます。

    戻り値にtrueを指定すると、ここで、イベントが処理されたことを意味しますので、 このイベントh、破棄されます。
    -> このイベントは、

  3. 通りすがり
    May 2nd, 2019 @ 11:44:33

    また、QPlainTextEditをQComboBoxの置き換えて使用することもできます。(基本的にQWidgetの派生クラスであれば、独自のクラスでも使えます)

    助詞と句点の位置がおかしいと思います。
    -> また、QPlainTextEditをQComboBoxに置き換えて使用することもできます(基本的にQWidgetの派生クラスであれば、独自のクラスでも使えます)。

Leave a Reply







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