Hello,

I have tried to make some sort with QTreeView. I have inherited QTreeView to
create a SortedTreeView. Then I've created a SortedListWidget class which
inherits QWidget class and which uses my SortedTreeView mapped to a
QStandardItemModel.
Then, if I directly run my SortedListWidget, and if I click on a line, do
some sort on colums, then my selected line is still correct.
However, if I run a QMainWindow in which my SortedListWidget is embedded,
and if I repeat the same actions, then my selected line is broken.
So my question is : why is it working directly with my SortedListWidget but
not if embedded in a QMainWindow?
(I'm using QtJambi 4.4.3_01)

Any help would be appreciated.

Vincent


Below test cases and code:

- Test cases:
Test 1:
1/ run SortedListWidget.main() : items are sorted in this order (A3 B3 C3 D3
for the 1st line, then A2 B2 C2 D2, and finally A1 B1 C1 D1)
2/ click on A3: the first line is selected (A3 B3 C3 D3)
3/ click on column C: the first line becomes the third one (A3 B3 C3 D3)
4/ click on C1: the first line is selected (A1 B1 C1 D1)
5/ click on column C: the first line becomes the third one (A1 B1 C1 D1)
=> it works fine !

Test 2:
1/ run MyMainWindow.main(): items are sorted in this order (A3 B3 C3 D3 for
the 1st line, then A2 B2 C2 D2, and finally A1 B1 C1 D1)
2/ click on A3: the first line is selected (A3 B3 C3 D3)
3/ click on column C: the first line becomes the third one (A3 B3 C3 D3)
4/ click on C1: the first line is selected (A1 B1 C1 D1)
5/ click on column C: the selected line is broken (A3 B1 C1 D3)
=> it doesn't work !


Here's my code:

--------- MyMainWindow -----------------
package org.workshop;

import com.trolltech.qt.gui.*;

public class MyMainWindow extends QMainWindow {

    Ui_MyMainWindow ui = new Ui_MyMainWindow();

    public static void main(String[] args) {
        QApplication.initialize(args);

        MyMainWindow testMyMainWindow = new MyMainWindow();
        testMyMainWindow.show();

        QApplication.exec();
    }

    public MyMainWindow() {
        ui.setupUi(this);
    }

    public MyMainWindow(QWidget parent) {
        super(parent);
        ui.setupUi(this);
    }
}

---------- Ui_MyMainWindow --------------
package org.workshop;

import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;

import org.workshop.*;

public class Ui_MyMainWindow
{
    public QWidget centralwidget;
    public SortedListWidget sortedListWidget;
    public QMenuBar menubar;
    public QStatusBar statusbar;

    public Ui_MyMainWindow() { super(); }

    public void setupUi(QMainWindow MyMainWindow)
    {
        MyMainWindow.setObjectName("MyMainWindow");
        MyMainWindow.resize(new QSize(496,
293).expandedTo(MyMainWindow.minimumSizeHint()));
        centralwidget = new QWidget(MyMainWindow);
        centralwidget.setObjectName("centralwidget");
        sortedListWidget = new SortedListWidget(centralwidget);
        sortedListWidget.setObjectName("sortedListWidget");
        sortedListWidget.setGeometry(new QRect(10, 10, 441, 261));
        MyMainWindow.setCentralWidget(centralwidget);
        menubar = new QMenuBar(MyMainWindow);
        menubar.setObjectName("menubar");
        menubar.setGeometry(new QRect(0, 0, 496, 22));
        MyMainWindow.setMenuBar(menubar);
        statusbar = new QStatusBar(MyMainWindow);
        statusbar.setObjectName("statusbar");
        MyMainWindow.setStatusBar(statusbar);
        retranslateUi(MyMainWindow);

        MyMainWindow.connectSlotsByName();
    } // setupUi

    void retranslateUi(QMainWindow MyMainWindow)
    {

MyMainWindow.setWindowTitle(com.trolltech.qt.core.QCoreApplication.translate("MyMainWindow",
"MainWindow"));
    } // retranslateUi

}


--------- SortedListWidget ------------
package org.workshop;

import com.trolltech.qt.core.Qt.Orientation;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QStandardItemModel;
import com.trolltech.qt.gui.QWidget;

public class SortedListWidget extends QWidget {

    Ui_SortedListWidget ui = new Ui_SortedListWidget();

    private QStandardItemModel model = null;

    public static void main(String[] args) {
        QApplication.initialize(args);

        SortedListWidget testSortedListWidget = new SortedListWidget();
        testSortedListWidget.show();

        QApplication.exec();
    }

    public SortedListWidget() {
        init();
    }

    public SortedListWidget(QWidget parent) {
        super(parent);
        init();
    }

    private void init(){
        ui.setupUi(this);
        model = createTransactionModel();
        populateModel();
        ui.sortedTreeView.setModel(model);
        ui.sortedTreeView.header().sectionClicked.connect(ui.sortedTreeView,
"doSort(java.lang.Integer)");
    }

    private QStandardItemModel createTransactionModel(){
        QStandardItemModel model = new QStandardItemModel(0, 4);

        model.setHeaderData(0, Orientation.Horizontal, tr("A"));
        model.setHeaderData(1, Orientation.Horizontal, tr("B"));
        model.setHeaderData(2, Orientation.Horizontal, tr("C"));
        model.setHeaderData(3, Orientation.Horizontal, tr("D"));

        return model;
    }

    private void populateModel(){
        addLine("A1", "B1", "C1", "D1");
        addLine("A2", "B2", "C2", "D2");
        addLine("A3", "B3", "C3", "D3");
    }

    protected void addLine(String dataA, String dataB, String dataC, String
dataD){
        int lastline = model.rowCount();
        model.insertRow(lastline);
        model.setData(model.index(lastline, 0), dataA);
        model.setData(model.index(lastline, 1), dataB);
        model.setData(model.index(lastline, 2), dataC);
        model.setData(model.index(lastline, 3), dataD);

    }

}

---------- Ui_SortedListWidget ---------------
package org.workshop;

import com.trolltech.qt.core.*;
import com.trolltech.qt.gui.*;

import org.workshop.*;

public class Ui_SortedListWidget
{
    public SortedTreeView sortedTreeView;

    public Ui_SortedListWidget() { super(); }

    public void setupUi(QWidget SortedListWidget)
    {
        SortedListWidget.setObjectName("SortedListWidget");
        SortedListWidget.resize(new QSize(400,
300).expandedTo(SortedListWidget.minimumSizeHint()));
        sortedTreeView = new SortedTreeView(SortedListWidget);
        sortedTreeView.setObjectName("sortedTreeView");
        sortedTreeView.setGeometry(new QRect(20, 30, 341, 192));
        sortedTreeView.setSortingEnabled(true);
        retranslateUi(SortedListWidget);

        SortedListWidget.connectSlotsByName();
    } // setupUi

    void retranslateUi(QWidget SortedListWidget)
    {

SortedListWidget.setWindowTitle(com.trolltech.qt.core.QCoreApplication.translate("SortedListWidget",
"Form"));
    } // retranslateUi

}

---------- SortedTreeView --------------
package org.workshop;

import com.trolltech.qt.core.Qt.SortOrder;
import com.trolltech.qt.gui.QTreeView;
import com.trolltech.qt.gui.QWidget;

public class SortedTreeView extends QTreeView {

    private SortOrder currentSortOrder = SortOrder.AscendingOrder;

    public SortedTreeView() {
        super();
    }

    public SortedTreeView(QWidget parent) {
        super(parent);
    }


    public void doSort(Integer column){
        if (column >= 0) {
            sortByColumn(column, currentSortOrder);
            changeSortOrder();
        }
    }

    private void changeSortOrder(){
        if (currentSortOrder.equals(SortOrder.AscendingOrder)) {
            currentSortOrder = SortOrder.DescendingOrder;
        }else{
            currentSortOrder = SortOrder.AscendingOrder;
        }
    }
}
_______________________________________________
Qt-jambi-interest mailing list
[email protected]
http://lists.trolltech.com/mailman/listinfo/qt-jambi-interest

Reply via email to