Difference between revisions of "Qtreewidget-bugs"

From ThorstensHome
Jump to: navigation, search
 
m (1 revision(s))
 

Latest revision as of 10:09, 18 October 2008

This is a testcase for qt's qtreewidget. It crashes if you have SortingEnabled(true).

State

This issue has been fixed in QT 4.4.1

Reproduce

CMakeLists.txt

project(ktimetracker)
find_package (KDE4 REQUIRED)
INCLUDE_DIRECTORIES( ${KDE4_INCLUDES} . )

macro_bool_to_01(X11_Xscreensaver_LIB HAVE_LIBXSS)


set(karm_shared_SRCS
        mainwindow.cpp
        task.cpp taskview.cpp
 )

set(karm_SRCS main.cpp ${karm_shared_SRCS})

kde4_add_app_icon(karm_SRCS "hi*-app-ktimetracker.png")

kde4_add_executable(ktimetracker ${karm_SRCS})

target_link_libraries(ktimetracker   ${KDE4_KIO_LIBS} ${KDE4_KCAL_LIBS} kdepim  kcal_resourceremote)
if(X11_Xscreensaver_LIB)
        target_link_libraries(ktimetracker  ${X11_Xscreensaver_LIB} )
endif(X11_Xscreensaver_LIB)

install(TARGETS ktimetracker  DESTINATION ${BIN_INSTALL_DIR} )


install( FILES karm_part.desktop  DESTINATION ${SERVICES_INSTALL_DIR})
install( FILES karmui.rc  DESTINATION ${DATA_INSTALL_DIR}/karmpart)
install( FILES karmui.rc  DESTINATION ${DATA_INSTALL_DIR}/ktimetracker)

kde4_install_icons( ${ICON_INSTALL_DIR}   )
install( FILES org.kde.ktimetracker.ktimetracker.xml DESTINATION ${KDE4_DBUS_INTERFACES_DIR} )

main.cpp

#include <QApplication>
#include "mainwindow.h"
#include "task.h"
#include "taskview.h"

int main( int argc, char *argv[] )
{
  QApplication* qapp=new QApplication(argc, argv);
  MainWindow *mainWindow;
  mainWindow = new MainWindow( QString() );
  mainWindow->show();
  qapp->exec();
  delete mainWindow;
  return 0;
}

mainwindow.h

#ifndef KARM_MAIN_WINDOW_H
#define KARM_MAIN_WINDOW_H

#include "taskview.h"
#include <QMainWindow>

class MainWindow : public QMainWindow
{
  Q_OBJECT

  private:
    TaskView *mainWidget;

  public:
    MainWindow( const QString &icsfile = "" );
    virtual ~MainWindow();

};

#endif // KARM_MAIN_WINDOW_H

mainwindow.cpp

#include <numeric>
#include <QDebug>
#include "mainwindow.h"
#include "taskview.h"

MainWindow::MainWindow( const QString &icsfile )
  :  QMainWindow( 0, Qt::WindowContextHelpButtonHint )
{
  mainWidget = new TaskView( this );
  setCentralWidget( mainWidget );
  mainWidget->addTask("name",0,0,0);
  mainWidget->addTask("hi1",0,0,mainWidget->itemAt(0));
  qDebug() << "still here";
  sleep(1);
  mainWidget->addTask("hi2",0,0,mainWidget->itemAt(0));
}

MainWindow::~MainWindow()
{
}

taskview.h

#ifndef KARM_TASK_VIEW_H
#define KARM_TASK_VIEW_H

#include <QTreeWidget>
class Task;

class TaskView : public QTreeWidget
{
  Q_OBJECT

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

    /**  Return the i'th item (zero-based), cast to a Task pointer.  */
    Task* itemAt( int i );

    /** Add a task to view and storage. */
    QString addTask( const QString& taskame, long total = 0, long session = 0, Task* parent = 0 );

};

#endif // KARM_TASK_VIEW

taskview.cpp

#include <QTreeWidget>
#include "taskview.h"
#include "task.h"

TaskView::TaskView( QWidget *parent ) : QTreeWidget(parent)
{
  setSortingEnabled( true );  // UNCOMMENTING THIS LINE PREVENTS CRASH
}

TaskView::~TaskView()
{
}

Task* TaskView::itemAt(int i)
{
  if ( topLevelItemCount() == 0 ) return 0;
  QTreeWidgetItemIterator item( this );
  while( *item && i-- ) ++item;
  if ( !( *item ) )
    return 0;
  else
    return (Task*)*item;
}

QString TaskView::addTask
( const QString& taskname, long total, long session, Task* parent )
{
  Task *task;
  static int i=0;
  if ( parent ) task = new Task( taskname, total, session, parent );
  else          task = new Task( taskname, total, session, this );
  setCurrentItem( task );
  return QString::number(i++);
}

task.h

#ifndef KARM_TASK_H
#define KARM_TASK_H

#include "taskview.h"

class Task : public QObject, public QTreeWidgetItem
{
  Q_OBJECT

  public:
    Task( const QString& taskame, long minutes, long sessionTime,
          Task* parent = 0);
    Task( const QString& taskame, long minutes, long sessionTime,
          TaskView* parent = 0);

    ~Task();
};

#endif // KARM_TASK_H

task.cpp

#include "task.h"

Task::Task( const QString& taskName, long minutes, long sessionTime,
            Task *parent)
  : QObject(), QTreeWidgetItem(parent)
{
}

Task::Task( const QString& taskName, long minutes, long sessionTime,
            TaskView *parent)
  : QObject(), QTreeWidgetItem(parent)
{
}

Task::~Task()
{
}

Compile, link and run

cmake . && make && ./ktimetracker

Problem:

Start the program and it crashes. Outcomment SortingEnabled and it crashes no longer.

filed at Trolltech with the number [Issue N213346] qtreeview crashes if sortingEnabled