pythonでfoobarのalternativeを作る 21 190530 version 7

2019/05/30 22:24 by yamasyuh68
  :追加された部分   :削除された部分
(差分が大きい場合、文字単位では表示しません)
pythonでfoobarのalternativeを作る 21
hoge


- とりあえずインクリメンタルはベタに実装してみる
- sqlexecute(line) はmodelを戻り値にして、treeへのセットは戻ってからにしてみよう
- とりあえずインクリメンタルはスレッドにせずベタに実装してみる
- sqlexecute(line) は再利用するためにmodelを戻り値にして、treeへのセットは戻ってからにしてみ

```
def incsearc():
  item=getevalue()
  temp=treeview.model()
  if item=='' :
     if temp == model1 :
          pass
     else:
          treeview.setmodel(model1)
          del temp
  else :
    line=''
    model=sqlexecute(line)
    setmodel(model)
    if temp != model1 :
        del temp
import sqlite3
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication,QWidget,QMainWindow
from PyQt5.QtGui import QStandardItemModel , QStandardItem
import ui 

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = ui.Ui_MainWindow() 
        self.ui.setupUi(self)

        self.db=r'e:\Programs\python\_研究\database\database.db'
        line='select artist ,album,title ,path from  musics order by artist ,album,title'
        self.model=self.sqlexecute(line)
        self.ui.treeView.setModel(self.model)

        self.ui.actionBye.triggered.connect(self.actionbye)
        self.ui.actionchange.triggered.connect(self.actionchange)
        self.ui.actionclear.triggered.connect(self.actionclear)
        self.ui.actionreload.triggered.connect(self.actionreload)

    def actionchange(self):
        model2 = QStandardItemModel()
        for i in range(3):
            parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i))
            model2.appendRow(parent1)
            for j in range(3):
                child1 = QStandardItem('Child {}'.format(i*3+j))
                child2 = QStandardItem('row: {}, col: {}'.format(i, j+1))
                child3 = QStandardItem('row: {}, col: {}'.format(i, j+2))
                parent1.appendRow([child1, child2, child3])
        self.ui.treeView.setModel(model2)

    def actionreload(self):
        pass

    def sqlexecute( self, line ):
        conn = sqlite3.connect( self.db )
        c = conn.cursor()
        item=['×','×']
        tree=[None,None,None,None]
        model = QStandardItemModel()
        for l in  c.execute(line) :
            # print(l)
            if item[0] != l[0] :      # 1 a-artist
                tree[1] = QStandardItem(l[0])
                model.appendRow(tree[1])
                item[0]=l[0]
            if item[1] != l[1] :      # 1 date album
                tree[2] = QStandardItem( l[1] )
                tree[1].appendRow(tree[2])
                item[1]=l[1]
            tree[3]=QStandardItem(l[2])
            tree[3].setData(l[3])
            tree[2].appendRow(tree[3])         # l[3] path 入れてない

        c.close()
        conn.close()
        return model

    def actionbye(self):
        quit()

    def actionclear(self):
        self.model.clear() 

    def search(self,e):
        item=self.ui.lineEdit.text()
        temp=self.ui.treeView.model()
        if item=='' :
            if temp == self.model :
                pass
            else:
                self.ui.treeView.setModel(self.model)
                del temp
        else :
            item="'%"+item+"%'"
            line='select artist ,album,title ,path from  musics  where \
                artist like {}  or path like {} or title like {}\
                order by artist ,album,title'.format(item,item,item)
            self.ui.treeView.setModel(self.sqlexecute(line))
            if temp != self.model:
                del temp

    def treeClicked(e):
        # listView
        pass

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    main_window = MainWindow()

    main_window.show()
    sys.exit(app.exec_())
```
  
※ 一文字も受け付けないようにしたら??感じは一文字でも良いんだけど

- 無事動作しました。ちょっと遅いかもだが気にならない程度
一文字入力したときの動作が遅い、まあそうだよな
一文字だけなら検索しないようにしたいけど、漢字は一文字でも良いから悩ましいところ
- 数字とアルファベットの一文字だけ検索しないようにしてみるか??正規表現が必要かな

-----
スレッド
- スレッド使ったパターン
```
init
  self.count=0
  self.th=[]

item=gettext()
if item=='' :
  m=getmodel()
  if m!=model1 :
    setmodel(1)
    del m
else :
  if count :
     self.th[self.count-1].stop()
     del self.th[self.count-1]
  self.th[self.count] = makemodel()
  self.th[self.count].start(self)
  self.count += 1 

class makemodel(threading.Thread):
  def __init__(self,parent)
     super(makemodel, self).__init__()
    self.par=par
  def run():



     item=par.gettext()
     sql 関係
     makemodei
          



```

- 途中です スレッドむずい( ̄― ̄?)

- 途中 スレッドむずい( ̄― ̄?)




      

hoge

  • とりあえずインクリメンタルはスレッドにせずベタに実装してみる
  • sqlexecute(line) は再利用するためにmodelを戻り値にして、treeへのセットは戻ってからにしてみる
import sqlite3
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication,QWidget,QMainWindow
from PyQt5.QtGui import QStandardItemModel , QStandardItem
import ui 

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = ui.Ui_MainWindow() 
        self.ui.setupUi(self)

        self.db=r'e:\Programs\python\_研究\database\database.db'
        line='select artist ,album,title ,path from  musics order by artist ,album,title'
        self.model=self.sqlexecute(line)
        self.ui.treeView.setModel(self.model)

        self.ui.actionBye.triggered.connect(self.actionbye)
        self.ui.actionchange.triggered.connect(self.actionchange)
        self.ui.actionclear.triggered.connect(self.actionclear)
        self.ui.actionreload.triggered.connect(self.actionreload)

    def actionchange(self):
        model2 = QStandardItemModel()
        for i in range(3):
            parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i))
            model2.appendRow(parent1)
            for j in range(3):
                child1 = QStandardItem('Child {}'.format(i*3+j))
                child2 = QStandardItem('row: {}, col: {}'.format(i, j+1))
                child3 = QStandardItem('row: {}, col: {}'.format(i, j+2))
                parent1.appendRow([child1, child2, child3])
        self.ui.treeView.setModel(model2)

    def actionreload(self):
        pass

    def sqlexecute( self, line ):
        conn = sqlite3.connect( self.db )
        c = conn.cursor()
        item=['×','×']
        tree=[None,None,None,None]
        model = QStandardItemModel()
        for l in  c.execute(line) :
            # print(l)
            if item[0] != l[0] :      # 1 a-artist
                tree[1] = QStandardItem(l[0])
                model.appendRow(tree[1])
                item[0]=l[0]
            if item[1] != l[1] :      # 1 date album
                tree[2] = QStandardItem( l[1] )
                tree[1].appendRow(tree[2])
                item[1]=l[1]
            tree[3]=QStandardItem(l[2])
            tree[3].setData(l[3])
            tree[2].appendRow(tree[3])         # l[3] path 入れてない

        c.close()
        conn.close()
        return model

    def actionbye(self):
        quit()

    def actionclear(self):
        self.model.clear() 

    def search(self,e):
        item=self.ui.lineEdit.text()
        temp=self.ui.treeView.model()
        if item=='' :
            if temp == self.model :
                pass
            else:
                self.ui.treeView.setModel(self.model)
                del temp
        else :
            item="'%"+item+"%'"
            line='select artist ,album,title ,path from  musics  where \
                artist like {}  or path like {} or title like {}\
                order by artist ,album,title'.format(item,item,item)
            self.ui.treeView.setModel(self.sqlexecute(line))
            if temp != self.model:
                del temp

    def treeClicked(e):
        # listView
        pass

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    main_window = MainWindow()

    main_window.show()
    sys.exit(app.exec_())

  • 無事動作しました。ちょっと遅いかもだが気にならない程度
    一文字入力したときの動作が遅い、まあそうだよな
    一文字だけなら検索しないようにしたいけど、漢字は一文字でも良いから悩ましいところ
  • 数字とアルファベットの一文字だけ検索しないようにしてみるか??正規表現が必要かな

  • スレッド使ったパターン
init
  self.count=0
  self.th=[]

item=gettext()
if item=='' :
  m=getmodel()
  if m!=model1 :
    setmodel(1)
    del m
else :
  if count :
     self.th[self.count-1].stop()
     del self.th[self.count-1]
  self.th[self.count] = makemodel()
  self.th[self.count].start(self)
  self.count += 1 

class makemodel(threading.Thread):
  def __init__(self,parent)
     super(makemodel, self).__init__()
    self.par=par
  def run():

     item=par.gettext()
     sql 関係
     makemodei
  • 途中です スレッドむずい( ̄― ̄?)