Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) QListWidgetExample3

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQListWidgetExample3/CppQListWidgetExample3.pro

 

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TEMPLATE = app

SOURCES += \
  main.cpp \
  dialog.cpp

HEADERS  += dialog.h

FORMS    += dialog.ui

 

 

 

 

 

./CppQListWidgetExample3/dialog.h

 

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
  class Dialog;
}

class Dialog : public QDialog
{
  Q_OBJECT

public:
  explicit Dialog(QWidget *parent = 0);
  ~Dialog();

private slots:
  void on_pushButton_clicked();

private:
  Ui::Dialog *ui;
  QTimer * const m_timer;

  void OnTimer();
};

#endif // DIALOG_H

 

 

 

 

 

./CppQListWidgetExample3/dialog.cpp

 

#include "dialog.h"

#include <QScrollBar>
#include <QTimer>

#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::Dialog),
  m_timer(new QTimer)
{
  ui->setupUi(this);
  ui->listWidget->setAlternatingRowColors(true);
  ui->listWidget->setWordWrap(true);

  QObject::connect(m_timer,&QTimer::timeout,this,&Dialog::OnTimer);
  m_timer->setInterval(1);
  m_timer->start();
}

Dialog::~Dialog()
{
  delete ui;
}

void Dialog::on_pushButton_clicked()
{
  QListWidgetItem * const item = new QListWidgetItem;
  item->setText("0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789 0123456789");
  ui->listWidget->addItem(item);
  this->repaint();
  m_timer->start();
}

void Dialog::OnTimer()
{
  if (ui->listWidget->verticalScrollBar()->isVisible())
  {
    const int height = ui->listWidget->height();
    ui->listWidget->setMinimumHeight(height + 1);
  }
  else
  {
    m_timer->stop();
  }
}

 

 

 

 

 

./CppQListWidgetExample3/main.cpp

 

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  Dialog w;
  w.show();

  return a.exec();
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml