2016-05-24 13 views
0

このコードはPyQt5 にあり、特定のデータベースに接続しています。 プログラムUIで行を選択するときに、行と列のインデックスを与える が必要です。 下の図の選択されたセルと同様に、私はその列と行のインデックスを知りたいだけです。リフレッシュのような特定の機能に入れてください。PyQt5選択インデックス

import sys 
from PyQt5.QtSql import QSqlQueryModel, QSqlDatabase, QSqlQuery 
from PyQt5.QtWidgets import * 
from PyQt5.QtCore import QModelIndex 
from PyQt5.QtGui import * 
import sqlite3 as lite 

############## 
i = 1 
app = QApplication(sys.argv) 
Own = QMainWindow() 
Own.resize(600, 600) 
############## 
db = QSqlDatabase.addDatabase("QSQLITE") 
db.setDatabaseName("projectDB.db") 
db.open() 
#### 

projectModel = QSqlQueryModel() 
projectModel.setQuery("select * from Contacts", db) 
#### 
projectView = QTableView(Own) 
projectView.resize(450, 450) 
projectView.move(20, 50) 
projectView.setModel(projectModel) 


######################### 
def New(): 
    New_N_Dialog = QDialog(Own) 
    New_N_Dialog.setWindowTitle("New") 
    New_N_Dialog.resize(700, 700) 
    ############## 
    Name_label = QLabel("Name : ", New_N_Dialog) 
    Name_label.resize(300, 20) 
    Name_label.move(20, 20) 
    ######## 
    Name_Entry = QLineEdit(New_N_Dialog) 
    Name_Entry.resize(300, 20) 
    Name_Entry.move(20, 50) 
    ############# 
    Mob_label = QLabel("Mobile : ", New_N_Dialog) 
    Mob_label.resize(300, 20) 
    Mob_label.move(20, 90) 
    ######## 
    Mob_Entry = QLineEdit(New_N_Dialog) 
    Mob_Entry.resize(300, 20) 
    Mob_Entry.move(20, 120) 
    ################# 
    Date_Label = QLabel("Date : ", New_N_Dialog) 
    Date_Label.resize(300, 20) 
    Date_Label.move(20, 150) 
    ######### 
    Date_Entry = QCalendarWidget(New_N_Dialog) 
    Date_Entry.resize(300, 200) 
    Date_Entry.move(20, 180) 

    ################ 
    Address_Label = QLabel("Address : ", New_N_Dialog) 
    Address_Label.resize(300, 20) 
    Address_Label.move(20, 400) 
    ######## 
    Address_Entry = QLineEdit(New_N_Dialog) 
    Address_Entry.resize(300, 20) 
    Address_Entry.move(20, 430) 
    ############### 
    Email_Label = QLabel("Email : ", New_N_Dialog) 
    Email_Label.resize(300, 20) 
    Email_Label.move(20, 460) 
    ####### 
    Email_Entry = QLineEdit(New_N_Dialog) 
    Email_Entry.resize(300, 20) 
    Email_Entry.move(20, 490) 
    ################ 
    Group_Label = QLabel("Group : ", New_N_Dialog) 
    Group_Label.resize(300, 20) 
    Group_Label.move(20, 520) 
    ###### 
    Group_Combo = QComboBox(New_N_Dialog) 
    Group_Combo.resize(300, 20) 
    Group_Combo.move(20, 550) 
    ############### 
    Con = lite.connect("projectDB.db") 
    Cur = Con.cursor() 
    Query = "select groupName from Groups" 
    Cur.execute(Query) 
    data = Cur.fetchall() 
    for x in range(len(data)): 
     Group_Combo.addItems(data[x]) 
    ########################## NOTES ##################### 
    Notes_Label = QLabel("Notes : ", New_N_Dialog) 
    Notes_Label.resize(300, 20) 
    Notes_Label.move(350, 20) 
    ################# 
    Notes_Ares = QLineEdit(New_N_Dialog) 
    Notes_Ares.resize(300, 520) 
    Notes_Ares.move(350, 50) 
    ####### Buttons ############# 
    Ok_But = QPushButton("OK", New_N_Dialog) 
    Ok_But.resize(90, 30) 
    Ok_But.move(550, 600) 

    def Ok_Pressed(): 
     Name_To_DB = Name_Entry.text() 
     Mob_To_DB = Mob_Entry.text() 
     Add_To_DB = Address_Entry.text() 
     Em_To_DB = Email_Entry.text() 
     Note_To_DB = Notes_Ares.text() 

     ###### Reformating ######### 

     Not_Modified = Date_Entry.selectedDate().toString() 
     Not_Modified1 = "%s" % Not_Modified 
     Date_list = Not_Modified1.split(" ") 
     del (Date_list[0]) 
     Final = '-'.join(Date_list) 
     # #########Group ComboBox Selecting ########## 
     # 
     Combo_Value = Group_Combo.currentIndex() + 1 
     ###### 
     print(Name_To_DB) 
     print(Mob_To_DB) 
     print(Add_To_DB) 
     print(Em_To_DB) 
     print(Note_To_DB) 
     print(Combo_Value) 
     print(Final) 
     projectModel.setQuery("insert into Contacts(fullname) values (%s)" % Name_To_DB, db) 
     projectModel.setQuery("insert into Contacts(mobile) values(%s)" % Mob_To_DB, db) ##Done 
     projectModel.setQuery("insert into Contacts(address) values(%s)" % Add_To_DB, db) 
     projectModel.setQuery("insert into Contacts(email) values(%s)" % Em_To_DB, db) 
     projectModel.setQuery("insert into Contacts(notes) values(%s)" % Note_To_DB, db) 
     projectModel.setQuery("insert into Contacts(groupID) values(%s)" % Combo_Value, db) ##Done 
     projectModel.setQuery("insert into Contacts(bdate) values(%s)" % Final, db) 

    Ok_But.clicked.connect(Ok_Pressed) 
    ######## 
    Can_But = QPushButton("Cancal", New_N_Dialog) 
    Can_But.resize(90, 30) 
    Can_But.move(440, 600) 

    def close_2(): 
     New_N_Dialog.close() 

    Can_But.clicked.connect(close_2) 
    ############### 
    New_N_Dialog.show() 


def New_Group(): 
    New_Dialog = QDialog(Own) 
    New_Dialog.setWindowTitle("New Group") 
    ############### 
    New_Dialog.resize(200, 200) 
    Label_1 = QLabel("Group ID : ", New_Dialog) 
    Label_1.resize(90, 20) 
    Label_1.move(25, 10) 
    #################### 
    Group_ID = QLineEdit(New_Dialog) 
    Group_ID.resize(150, 20) 
    Group_ID.move(25, 40) 
    print(Group_ID.text()) 
    ############## 
    Label_2 = QLabel("Group Name : ", New_Dialog) 
    Label_2.resize(150, 20) 
    Label_2.move(25, 80) 
    ################ 
    Group_Name = QLineEdit(New_Dialog) 
    Group_Name.resize(150, 20) 
    Group_Name.move(25, 120) 
    ################ 
    Ok_But = QPushButton("OK", New_Dialog) 
    Ok_But.resize(50, 20) 
    Ok_But.move(125, 150) 

    def Adding(): 
     G_ID = Group_ID.text() 
     projectModel.setQuery("insert into Groups(groupID) values(%s)" % G_ID, db) 
     print(Group_Name.text()) 
     G_Name = Group_Name.text() 
     projectModel.setQuery("insert into Groups(groupName) values(%s)" % G_Name, db) 

    Ok_But.clicked.connect(Adding) 
    ################ 
    Cancal_But = QPushButton("Cancal", New_Dialog) 
    Cancal_But.resize(50, 20) 
    Cancal_But.move(25, 150) 
    New_Dialog.show() 

    def close(): 
     New_Dialog.close() 

    Cancal_But.clicked.connect(close) 


def Edit(): 
    New_N_Dialog = QDialog(Own) 
    New_N_Dialog.setWindowTitle("New") 
    New_N_Dialog.resize(700, 700) 
    ############## 
    Name_label = QLabel("Name : ", New_N_Dialog) 
    Name_label.resize(300, 20) 
    Name_label.move(20, 20) 
    ######## 
    Name_Entry = QLineEdit(New_N_Dialog) 
    Name_Entry.resize(300, 20) 
    Name_Entry.move(20, 50) 
    ############# 
    Mob_label = QLabel("Mobile : ", New_N_Dialog) 
    Mob_label.resize(300, 20) 
    Mob_label.move(20, 90) 
    ######## 
    Mob_Entry = QLineEdit(New_N_Dialog) 
    Mob_Entry.resize(300, 20) 
    Mob_Entry.move(20, 120) 
    ################# 
    Date_Label = QLabel("Date : ", New_N_Dialog) 
    Date_Label.resize(300, 20) 
    Date_Label.move(20, 150) 
    ######### 
    Date_Entry = QCalendarWidget(New_N_Dialog) 
    Date_Entry.resize(300, 200) 
    Date_Entry.move(20, 180) 

    ################ 
    Address_Label = QLabel("Address : ", New_N_Dialog) 
    Address_Label.resize(300, 20) 
    Address_Label.move(20, 400) 
    ######## 
    Address_Entry = QLineEdit(New_N_Dialog) 
    Address_Entry.resize(300, 20) 
    Address_Entry.move(20, 430) 
    ############### 
    Email_Label = QLabel("Email : ", New_N_Dialog) 
    Email_Label.resize(300, 20) 
    Email_Label.move(20, 460) 
    ####### 
    Email_Entry = QLineEdit(New_N_Dialog) 
    Email_Entry.resize(300, 20) 
    Email_Entry.move(20, 490) 
    ################ 
    Group_Label = QLabel("Group : ", New_N_Dialog) 
    Group_Label.resize(300, 20) 
    Group_Label.move(20, 520) 
    ###### 
    Group_Combo = QComboBox(New_N_Dialog) 
    Group_Combo.resize(300, 20) 
    Group_Combo.move(20, 550) 
    ############### 
    Con = lite.connect("projectDB.db") 
    Cur = Con.cursor() 
    Query = "select groupName from Groups" 
    Cur.execute(Query) 
    data = Cur.fetchall() 
    for x in range(len(data)): 
     Group_Combo.addItems(data[x]) 
    ########################## NOTES ##################### 
    Notes_Label = QLabel("Notes : ", New_N_Dialog) 
    Notes_Label.resize(300, 20) 
    Notes_Label.move(350, 20) 
    ################# 
    Notes_Ares = QLineEdit(New_N_Dialog) 
    Notes_Ares.resize(300, 520) 
    Notes_Ares.move(350, 50) 
    ####### Buttons ############# 
    Ok_But = QPushButton("OK", New_N_Dialog) 
    Ok_But.resize(90, 30) 
    Ok_But.move(550, 600) 

    def Ok_Pressed(): 
     Name_To_DB = Name_Entry.text() 
     Mob_To_DB = Mob_Entry.text() 
     Add_To_DB = Address_Entry.text() 
     Em_To_DB = Email_Entry.text() 
     Note_To_DB = Notes_Ares.text() 

     ###### Reformating ######### 

     Not_Modified = Date_Entry.selectedDate().toString() 
     Not_Modified1 = "%s" % Not_Modified 
     Date_list = Not_Modified1.split(" ") 
     del (Date_list[0]) 
     Final = '-'.join(Date_list) 
     # #########Group ComboBox Selecting ########## 
     # 
     Combo_Value = Group_Combo.currentIndex() + 1 
     ###### 
     print(Name_To_DB) 
     print(Mob_To_DB) 
     print(Add_To_DB) 
     print(Em_To_DB) 
     print(Note_To_DB) 
     print(Combo_Value) 
     print(Final) 
     projectModel.setQuery('''update Contacts 
           set fullname = "%s" 
           where ''' % Name_To_DB, db) 
     # projectModel.setQuery("insert into Contacts(mobile) values(%s)" % Mob_To_DB, db) ##Done 
     # projectModel.setQuery("insert into Contacts(address) values(%s)" % Add_To_DB, db) 
     # projectModel.setQuery("insert into Contacts(email) values(%s)" % Em_To_DB, db) 
     # projectModel.setQuery("insert into Contacts(notes) values(%s)" % Note_To_DB, db) 
     # projectModel.setQuery("insert into Contacts(groupID) values(%s)" % Combo_Value, db) ##Done 
     # projectModel.setQuery("insert into Contacts(bdate) values(%s)" % Final, db) 

    Ok_But.clicked.connect(Ok_Pressed) 
    ######## 
    Can_But = QPushButton("Cancal", New_N_Dialog) 
    Can_But.resize(90, 30) 
    Can_But.move(440, 600) 

    def close_2(): 
     New_N_Dialog.close() 

    Can_But.clicked.connect(close_2) 
    ############### 
    New_N_Dialog.show() 


def Remove(): 
    print("fFFFFK") 


def Refresh(): 
    ss = QListView() 
    af = QModelIndex() 
    print(af) 
def fk(): 
    print("asdjashd") 


###################### 
def First(): 
    projectModel.setQuery("select * FROM Contacts WHERE contactID = 1", db) 


def Prev(): 
    global i 
    i = i - 1 
    projectModel.setQuery("select * FROM Contacts WHERE contactID = '%d'-1" % (i), db) 


def Next(): 
    global i 
    i = i + 1 
    projectModel.setQuery("select * FROM Contacts WHERE contactID = '%d'+1" % (i), db) 


def Last(): 
    projectModel.setQuery("select * FROM Contacts WHERE contactID = 100", db) 


####################### 
def Quit(): 
    sys.exit() 


######################## 
def Reset_Table(): 
    projectModel.setQuery("SELECT * FROM Contacts", db) 


New_But = QPushButton("New", Own) 
New_But.move(490, 50) 
New_But.resize(90, 30) 
New_But.clicked.connect(New) 
################### 
New_Group_But = QPushButton("New Group", Own) 
New_Group_But.move(490, 90) 
New_Group_But.resize(90, 30) 
New_Group_But.clicked.connect(New_Group) 
################ 
Edit_But = QPushButton("Edit", Own) 
Edit_But.move(490, 130) 
Edit_But.resize(90, 30) 
Edit_But.clicked.connect(Edit) 
############# 
Remove_But = QPushButton("Remove", Own) 
Remove_But.move(490, 170) 
Remove_But.resize(90, 30) 
Remove_But.clicked.connect(Remove) 
############## 
Refresh_But = QPushButton("Refresh", Own) 
Refresh_But.move(490, 210) 
Refresh_But.resize(90, 30) 
Refresh_But.clicked.connect(Refresh) 
######################### 
First_But = QPushButton("First", Own) 
First_But.move(20, 520) 
First_But.resize(90, 30) 
First_But.clicked.connect(First) 
################ 
Prev_But = QPushButton("Previous", Own) 
Prev_But.move(120, 520) 
Prev_But.resize(90, 30) 
Prev_But.clicked.connect(Prev) 
####################3 
Next_But = QPushButton("Next", Own) 
Next_But.move(220, 520) 
Next_But.resize(90, 30) 
Next_But.clicked.connect(Next) 
################# 
Last_But = QPushButton("Last", Own) 
Last_But.move(320, 520) 
Last_But.resize(90, 30) 
Last_But.clicked.connect(Last) 
########## Quit ############ 
Quit_But = QPushButton("Quit", Own) 
Quit_But.move(490, 420) 
Quit_But.resize(90, 30) 
Quit_But.clicked.connect(Quit) 
###################### 
For_R = Own.menuBar() 
Reset = For_R.addMenu('&Options') 

Reset_Opt = QAction(QIcon('exit_icon.png'), 'Reset', Own) 
Reset_Opt.setShortcut('Ctrl+R') 
Reset_Opt.triggered.connect(Reset_Table) 
Reset.addAction(Reset_Opt) 
############### 

######## 




############# 
Own.show() 
app.exec_() 

enter image description here

答えて

0

OKありがとう、私はあなたのためにそれをチェックして、それはこのように書き:これは本当に質問に答えていない

tableWidget = QtWidgets.QTableWidget() 
tableWidget.clicked.connect(show_row) 

def show_row():  
    print(listWidget.currentRow()) 
+0

。別の質問がある場合は、[Ask Question](http://stackoverflow.com/questions/ask)をクリックして質問することができます。十分な[評判](http://stackoverflow.com/help/)があれば、[賞金を追加する](http://stackoverflow.com/help/privileges/set-bounties)でもこの質問にもっと注意を払うことができます何が評判か)。 - [レビューから](/レビュー/低品質の投稿/ 13857681) – Tempux

+0

私は自分の答えを変更しました。私はこれがあなたが探しているものでなければならないと思う – saitam

関連する問題