Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QAbstractItemModel #24

Closed
5k3105 opened this issue Apr 20, 2016 · 17 comments
Closed

QAbstractItemModel #24

5k3105 opened this issue Apr 20, 2016 · 17 comments

Comments

@5k3105
Copy link
Contributor

5k3105 commented Apr 20, 2016

Is this possible or should I just use the Tree/Table Widget?

@therecipe
Copy link
Owner

It's currently not possible, because it's an abstract class and I need to implement all missing pure virtual functions first.

So, it will take some time.

@5k3105
Copy link
Contributor Author

5k3105 commented Apr 22, 2016

No prob. I will use the widget versions of Tree and Table. Thanks!

@therecipe
Copy link
Owner

It's implemented now c0571ee

@5k3105
Copy link
Contributor Author

5k3105 commented Apr 28, 2016

Oh wow. You are working faster than me! I will try to test this soon. Thanks!

@5k3105
Copy link
Contributor Author

5k3105 commented Apr 28, 2016

I just updated to latest version. QTableWidget seems to be broken:

    package main

    import (
        "os"

        "github.com/therecipe/qt/widgets"
    )

    func main() {
        widgets.NewQApplication(len(os.Args), os.Args)

        // Main Window
        var window = widgets.NewQMainWindow(nil, 0)

        // Main Widget
        mw := widgets.NewQTableWidget2(12, 3, nil)

        // Set Central Widget
        window.SetCentralWidget(mw)

        // Run App
        widgets.QApplication_SetStyle2("fusion")
        window.ShowMaximized()
        widgets.QApplication_Exec()
    }

@5k3105
Copy link
Contributor Author

5k3105 commented Apr 28, 2016

Maybe it is that I am using 1.5 instead of 1.6? I will update and check. Thanks.

@5k3105
Copy link
Contributor Author

5k3105 commented Apr 28, 2016

That didn't seem to help. Still broken. Notice something I think is funny - widgets takes 0 sec and sql takes 13min:

C:\goworksp\src\github.com\therecipe\qt (master)
λ setup

go run ./internal/setup/check.go

if 0 NEQ 0 exit /b 0

go run C:\goworksp/src/github.com/therecipe/qt/internal/setup/generate.go
________________________Generate________________________
core                     funcs:  4092 (-  394)   13s
androidextras            funcs:    45 (-    0)   0
gui                      funcs: 18938 (-  437)   10s
network                  funcs:   661 (-  155)   2s
sql                      funcs:   330 (-   23)   1s
xml                      funcs:   397 (-   18)   0
dbus                     funcs:   195 (-   21)   0
nfc                      funcs:   114 (-   28)   0
script                   funcs:   210 (-   24)   0
sensors                  funcs:   296 (-    9)   2s
positioning              funcs:   193 (-   26)   0
widgets                  funcs:  5312 (-  190)   40s
qml                      funcs:   285 (-   25)   1s
websockets               funcs:    80 (-   16)   0
xmlpatterns              funcs:   177 (-   23)   0
bluetooth                funcs:   239 (-   47)   1s
webchannel               funcs:    13 (-    2)   0
svg                      funcs:    64 (-    1)   0
multimedia               funcs:  1210 (-   65)   6s
quick                    funcs:   484 (-   40)   2s
help                     funcs:    91 (-   10)   2s
location                 funcs:   520 (-   19)   0
scripttools              funcs:    14 (-    0)   0
multimediawidgets        funcs:    64 (-    0)   0
uitools                  funcs:    17 (-    0)   0

if 0 NEQ 0 exit /b 0

go run C:\goworksp/src/github.com/therecipe/qt/internal/setup/install.go
________________________Install-desktop________________________
core                           1m24s
androidextras                  1s
gui                            1m5s
network                        20s
sql                            13m51s
xml                            9s
dbus                           7s
nfc                            6s
script                         6s
sensors                        17s
positioning                    6s
widgets                        0
qml                            10s
websockets                     5s
xmlpatterns                    6s
bluetooth                      10s
webchannel                     3s
svg                            10s
multimedia                     51s
quick                          23s
help                           19s
location                       6s
scripttools                    4s
multimediawidgets              13s
uitools                        5s

if 0 NEQ 0 exit /b 0

go run C:\goworksp/src/github.com/therecipe/qt/internal/setup/test.go
_________________________Test-desktop__________________________
widgets\line_edits             43s
widgets\video_player           58s
widgets\graphicsscene          40s
quick\bridge                   54s
quick\bridge2                  56s
quick\calc                     53s
quick\dialog                   50s
quick\translate                45s
quick\view                     46s
qml\application                18s
qml\prop                       10s
uitools\calculator             40s

@therecipe
Copy link
Owner

Hey

Yes, some functions were missing and therefore QTableWidget didn't work.
And the sql module depends on the widgets module now, that's the reason it takes so long now ^^
(Hopefully go 1.7 will significant reduce the time it takes to install the widgets module)

Also the QTableWidget should work now: 5fb71be

@5k3105
Copy link
Contributor Author

5k3105 commented Apr 30, 2016

Thanks!

@therecipe
Copy link
Owner

You are welcome.

@5k3105
Copy link
Contributor Author

5k3105 commented May 18, 2016

Hi :)

First - very impressed with the 5.6 upgrade which didn't break any of my code! Thank you!

Finally getting around to testing QAbstractItemModel :

    package main

    import (
        "os"

        "github.com/therecipe/qt/core"
        "github.com/therecipe/qt/gui"
        "github.com/therecipe/qt/widgets"
    )

    type delegate struct {
        *widgets.QStyledItemDelegate
    }

    func main() {
        widgets.NewQApplication(len(os.Args), os.Args)

        // Main Window
        var window = widgets.NewQMainWindow(nil, 0)
        window.SetWindowTitle("--")

        // Main Widget
        view := widgets.NewQTableView(nil)
        model := gui.NewQStandardItemModel2(2, 2, nil)
        view.SetModel(model)

        delegate := NewDelegate()
        view.SetItemDelegate(delegate)

        // Set Central Widget
        window.SetCentralWidget(view)

        // Run App
        widgets.QApplication_SetStyle2("fusion")
        window.ShowMaximized()
        widgets.QApplication_Exec()
    }

    func NewDelegate() *widgets.QStyledItemDelegate {
        item := widgets.NewQStyledItemDelegate(nil)
        item.ConnectCreateEditor(createEditor)
        item.ConnectSetEditorData(setEditorData)
        item.ConnectSetModelData(setModelData)
        item.ConnectUpdateEditorGeometry(updateEditorGeometry)
    }

    func (d *delegate) createEditor(parent widgets.QWidget_ITF, option widgets.QStyleOptionViewItem_ITF, index core.QModelIndex_ITF) *widgets.QWidget {

        editor := widgets.NewQSpinBox(nil)
        editor.SetMinimum(0)
        editor.SetMaximum(100)

        return editor
    }

    func (d *delegate) setEditorData(editor widgets.QWidget_ITF, index core.QModelIndex_ITF) {
        value := index.Model().Data(index, 2).ToInt() // 2 = Qt::EditRole
        editor.SetValue(value)
    }

    func (d *delegate) setModelData(editor widgets.QWidget_ITF, model core.QAbstractItemModel_ITF, index core.QModelIndex_ITF) {
        editor.InterpretText()
        value := editor.Value()
        model.SetData(index, value, 2) // 2 = Qt::EditRole
    }

    func (d *delegate) updateEditorGeometry(editor widgets.QWidget_ITF, option widgets.QStyleOptionViewItem_ITF, index core.QModelIndex_ITF) {
        editor.SetGeometry(option.Rect)
    }

I get a lot of errors. Following from this example: http://www.bogotobogo.com/Qt/Qt5_QTableView_QItemDelegate_ModelView_MVC.php

Could you take a look when you get a chance?

Thanks!

@therecipe
Copy link
Owner

Hey

Thank you :)
I tried to keep the api as stable as possible even through there have been a few minor changes like different overload-numbers and such.

The errors you saw, are from a bug in the qtmoc tool (which creates all the moc* files)
The tool is needed if you anonymously subclass Qt classes like you did with the Delegate class:

type Delegate struct {
    widgets.QStyledItemDelegate //don't use *pointers or it won't work
}

The qtmoc tool will create a lot of functions so you can use the Delegate class just like you would use the widgets.QStyledItemDelegate class (take a look at the moc.go file)
Also you can take a look at the quick/bridges examples to see how you can use this to add you own signals and slots.

The qtmoc tool is executed each time you run the qtdeploy tool, but you can also run it manually.

If you don't want to use the qtmoc tool, you would have to define your Delegate class similar to this

type Delegate struct {
    item widgets.QStyledItemDelegate
}

(just don't use Qt classes in anonymous fields)

Sorry, this got a little longer than I expected.
But, I haven't written an explanation about the use of the qtmoc tool down yet.
So I assumed you subclassed and therefore triggered the qtmoc tool by accident.

I got your QAbstractItemModel example working with this commit 9476e3f:

package main

import (
    "os"

    "github.com/therecipe/qt/core"
    "github.com/therecipe/qt/gui"
    "github.com/therecipe/qt/widgets"
)

type Delegate struct {
    widgets.QStyledItemDelegate //don't use *pointers or it won't work
}

func main() {
    widgets.NewQApplication(len(os.Args), os.Args)

    // Main Window
    var window = widgets.NewQMainWindow(nil, 0)
    window.SetWindowTitle("--")

    // Main Widget
    view := widgets.NewQTableView(nil)
    model := gui.NewQStandardItemModel2(2, 2, nil)
    view.SetModel(model)

    delegate := InitDelegate()
    view.SetItemDelegate(delegate)

    // Set Central Widget
    window.SetCentralWidget(view)

    // Run App
    widgets.QApplication_SetStyle2("fusion")
    window.Show()
    widgets.QApplication_Exec()
}

func InitDelegate() *Delegate {
    item := NewDelegate(nil) //will be generated in moc.go
    item.ConnectCreateEditor(createEditor)
    item.ConnectSetEditorData(setEditorData)
    item.ConnectSetModelData(setModelData)
    item.ConnectUpdateEditorGeometry(updateEditorGeometry)
    return item
}

func createEditor(parent *widgets.QWidget, option *widgets.QStyleOptionViewItem, index *core.QModelIndex) *widgets.QWidget {

    editor := widgets.NewQSpinBox(parent)
    editor.SetMinimum(0)
    editor.SetMaximum(100)
    return editor.QWidget_PTR()
}

func setEditorData(editor *widgets.QWidget, index *core.QModelIndex) {

    value := index.Model().Data(index, int(core.Qt__EditRole)).ToInt(true)
    spinbox := widgets.NewQSpinBoxFromPointer(editor.Pointer()) //like static_cast

    spinbox.SetValue(value)
}

func setModelData(editor *widgets.QWidget, model *core.QAbstractItemModel, index *core.QModelIndex) {

    spinbox := widgets.NewQSpinBoxFromPointer(editor.Pointer()) //like static_cast
    spinbox.InterpretText()

    value := spinbox.Value()
    model.SetData(index, core.NewQVariant7(value), int(core.Qt__EditRole))
}

func updateEditorGeometry(editor *widgets.QWidget, option *widgets.QStyleOptionViewItem, index *core.QModelIndex) {
    editor.SetGeometry(option.Rect())
}

@5k3105
Copy link
Contributor Author

5k3105 commented May 23, 2016

It works :)
Thanks. That is a huge amount of work on that change. Amazing work.
If you are ever in the Boulder/Denver CO area let me know so I can buy you a beer (at the least).

Thanks!!

@5k3105
Copy link
Contributor Author

5k3105 commented Jun 6, 2016

Hi,

I'm getting:

argstable.go:25: undefined: NewDelegate

    package argstable

    import (
        "github.com/therecipe/qt/core"
        "github.com/therecipe/qt/gui"
        "github.com/therecipe/qt/widgets"
    )

    type Delegate struct {
        widgets.QStyledItemDelegate //don't use *pointers or it won't work
    }

    func NewArgsTable() *widgets.QTableView {
        view := widgets.NewQTableView(nil)
        model := gui.NewQStandardItemModel2(2, 1, nil)
        view.SetModel(model)

        delegate := InitDelegate()
        view.SetItemDelegate(delegate)

        return view
    }

    func InitDelegate() *Delegate {
        item := NewDelegate(nil) //will be generated in moc.go
        item.ConnectCreateEditor(createEditor)
        item.ConnectSetEditorData(setEditorData)
        item.ConnectSetModelData(setModelData)
        item.ConnectUpdateEditorGeometry(updateEditorGeometry)
        return item
    }

    func createEditor(parent *widgets.QWidget, option *widgets.QStyleOptionViewItem, index *core.QModelIndex) *widgets.QWidget {

        editor := widgets.NewQLineEdit(parent)
        //  editor.SetMinimum(0)
        //  editor.SetMaximum(100)
        return editor.QWidget_PTR()
    }

    func setEditorData(editor *widgets.QWidget, index *core.QModelIndex) {

        value := index.Model().Data(index, int(core.Qt__EditRole)).ToString() //ToInt(true)
        lineedit := widgets.NewQLineEditFromPointer(editor.Pointer())         //like static_cast

        lineedit.SetText(value)
    }

    func setModelData(editor *widgets.QWidget, model *core.QAbstractItemModel, index *core.QModelIndex) {

        lineedit := widgets.NewQLineEditFromPointer(editor.Pointer()) //like static_cast

        text := lineedit.Text()
        model.SetData(index, core.NewQVariant14(text), int(core.Qt__EditRole))

        //  lineedit.InterpretText()

        //  value := lineedit.Value()
        //  model.SetData(index, core.NewQVariant7(value), int(core.Qt__EditRole))
    }

    func updateEditorGeometry(editor *widgets.QWidget, option *widgets.QStyleOptionViewItem, index *core.QModelIndex) {
        editor.SetGeometry(option.Rect())
    }

@therecipe
Copy link
Owner

Hey

Currently you need to cd into the folder (argstable) and run qtmoc.
Because qtmoc is only executed in the main folder, when it's invoked through qtdeploy.

I will fix this and post here, once it's done.

@therecipe
Copy link
Owner

It should be fixed now a6a0643

@5k3105
Copy link
Contributor Author

5k3105 commented Jun 22, 2016

That works. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants