Refactor chat widget to use QTextBrower instead of QListWidget:
authorunc0rr
Fri, 28 Nov 2008 16:45:31 +0000
changeset 1520 f72f538eba05
parent 1519 7b6adbe5266a
child 1521 971f523d7c01
Refactor chat widget to use QTextBrower instead of QListWidget: - Fixes visual glitches when the string is too long - Allows to select text
QTfrontend/chatwidget.cpp
QTfrontend/chatwidget.h
--- a/QTfrontend/chatwidget.cpp	Fri Nov 28 14:52:00 2008 +0000
+++ b/QTfrontend/chatwidget.cpp	Fri Nov 28 16:45:31 2008 +0000
@@ -16,6 +16,7 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  */
 
+#include <QTextBrowser>
 #include <QListWidget>
 #include <QLineEdit>
 #include <QAction>
@@ -37,8 +38,8 @@
 
   mainLayout.addWidget(chatEditLine, 1, 0, 1, 2);
 
-  chatText = new QListWidget(this);
-  chatText->setMinimumHeight(10);
+  chatText = new QTextBrowser(this);
+  chatText->setMinimumHeight(20);
   chatText->setMinimumWidth(10);
   chatText->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
   mainLayout.addWidget(chatText, 0, 0);
@@ -63,14 +64,14 @@
 
 void HWChatWidget::onChatString(const QString& str)
 {
-	QListWidget* w = chatText;
+	if (chatStrings.size() > 250)
+		chatStrings.removeFirst();
 	
-	if (w->count() > 250)
-		delete w->item(0);
+	chatStrings.append(str);
+	
+	chatText->setPlainText(chatStrings.join("\n"));
 
-	w->addItem(str);
-	w->scrollToBottom();
-	w->setSelectionMode(QAbstractItemView::NoSelection);
+	chatText->moveCursor(QTextCursor::End);
 }
 
 void HWChatWidget::nickAdded(const QString& nick)
@@ -81,16 +82,17 @@
 
 void HWChatWidget::nickRemoved(const QString& nick)
 {
-  QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly);
-  for(QList<QListWidgetItem *>::iterator it=items.begin(); it!=items.end();) {
-    chatNicks->takeItem(chatNicks->row(*it));
-    ++it;
-  }
+	QList<QListWidgetItem *> items = chatNicks->findItems(nick, Qt::MatchExactly);
+	for(QList<QListWidgetItem *>::iterator it=items.begin(); it!=items.end();) {
+		chatNicks->takeItem(chatNicks->row(*it));
+		++it;
+	}
 }
 
 void HWChatWidget::clear()
 {
 	chatText->clear();
+	chatStrings.clear();
 	chatNicks->clear();
 }
 
--- a/QTfrontend/chatwidget.h	Fri Nov 28 14:52:00 2008 +0000
+++ b/QTfrontend/chatwidget.h	Fri Nov 28 16:45:31 2008 +0000
@@ -23,8 +23,9 @@
 #include <QString>
 #include <QGridLayout>
 
+class QTextBrowser;
+class QLineEdit;
 class QListWidget;
-class QLineEdit;
 
 class HWChatWidget : public QWidget
 {
@@ -46,7 +47,8 @@
 
  private:
   QGridLayout mainLayout;
-  QListWidget* chatText;
+  QTextBrowser* chatText;
+  QStringList chatStrings;
   QListWidget* chatNicks;
   QLineEdit* chatEditLine;