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.
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.



Add to your favorites(bookmarks): はてなブックマークへ追加するdel.icio.usLivedoor ClipYahoo!FC2Nifty ClipPOOKMARK. AirlinesBuzzurl(バザール)Choixnewsing

Trackback URL

After Admin approves this comment, it will be shown.


Comments

Leave a Reply





*




Contents

Recent Posts


Tag Cloud

Links

Site Description

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

Add to your favorites(bookmarks)

はてなブックマークへ追加するdel.icio.usLivedoor ClipYahoo!FC2Nifty ClipPOOKMARK. AirlinesBuzzurl(バザール)Choixnewsing