質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

6219閲覧

PyQtにおけるQWidgetとQGraphicsViewの組み合わせ

wabisuke2718

総合スコア96

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/04/08 14:54

http://d.hatena.ne.jp/mFumi/20141112/1415806010

このサイトをお手本にQWidgetとQGraphicsViewの組み合わせを実現しようとしているのですが、うまくいきません。

ウィンドウ左側に碁盤のマス目(9×9)、右側にボタンを3つ配置しようとしています。

コードも見比べてみましたが、pyqtを触り始めて間もないので良く分かりません。

もう一つ参考にしているサイトは次

http://zetcode.com/gui/pyqt5/

コードを貼りますので、どこを直したらよいかご教授お願い致します。

#!/usr/bin/env python from PyQt5.QtCore import (QLineF, QPointF, QRectF, Qt) from PyQt5.QtGui import (QBrush, QColor, QPainter) from PyQt5.QtWidgets import (QWidget, QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem, QGridLayout, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton) import numpy as np class TicTacToe(QGraphicsItem): def __init__(self): super(TicTacToe, self).__init__() self.board = [[-1, -1, -1],[-1, -1, -1], [-1, -1, -1]] self.goban = np.zeros((11, 11)) self.O = 0 self.X = 1 self.turn = self.O self.kuro = 1 self.siro = -1 self.space = 0 self.now = self.kuro def reset(self): for y in range(11): for x in range(11): self.board[y][x] = -1 self.goban[y][x] = 0 self.turn = self.O self.update() def select(self, x, y): if x < 1 or y < 1 or x > 9 or y > 9: return """ if self.board[y][x] == -1: self.board[y][x] = self.turn self.turn = 1 - self.turn """ #とりあえず self.goban[y][x] = self.kuro def paint(self, painter, option, widget): painter.setPen(Qt.black) """ painter.drawLine(0,100,300,100) painter.drawLine(0,200,300,200) painter.drawLine(100,0,100,300) painter.drawLine(200,0,200,300) """ self.goban_left_top_x = 30 self.goban_left_top_y = 30 self.length_of_between = 30 self.goban_line_num = 9 for x in range(self.goban_left_top_x, self.goban_left_top_x + self.length_of_between * self.goban_line_num, self.length_of_between): painter.drawLine(x, self.goban_left_top_y, x, self.goban_left_top_y + self.length_of_between * (self.goban_line_num - 1)) for y in range(self.goban_left_top_y, self.goban_left_top_y + self.length_of_between * self.goban_line_num, self.length_of_between): painter.drawLine(self.goban_left_top_x, y, self.goban_left_top_x + self.length_of_between * (self.goban_line_num - 1), y) """ for y in range(3): for x in range(3): if self.board[y][x] == self.O: painter.setPen(Qt.red) painter.drawEllipse(QPointF(50+x*100, 50+y*100), 30, 30) elif self.board[y][x] == self.X: painter.setPen(Qt.blue) painter.drawLine(20+x*100, 20+y*100, 80+x*100, 80+y*100) painter.drawLine(20+x*100, 80+y*100, 80+x*100, 20+y*100) """ for y in range(1, 10): for x in range(1, 10): cross_point_x = self.goban_left_top_x + self.length_of_between * (x - 1) cross_point_y = self.goban_left_top_y + self.length_of_between * (y - 1) if self.goban[y][x] == self.kuro: painter.setPen(Qt.red) painter.drawEllipse(QPointF(cross_point_x, cross_point_y), 10, 10) elif self.goban[y][x] == self.siro: #とりあえず pass def boundingRect(self): return QRectF(0,0,300,300) def mousePressEvent(self, event): pos = event.pos() #self.select(int(pos.x()/100), int(pos.y()/100)) hantei_ryouiki_satan = int(self.goban_left_top_x - int(self.length_of_between / 2)) x_from_hantei_ryouiki_satan = int(pos.x()) - hantei_ryouiki_satan hantei_ryouiki_joutan = int(self.goban_left_top_y - int(self.length_of_between / 2)) y_from_hantei_ryouiki_joutan = int(pos.y()) - hantei_ryouiki_joutan x = int(x_from_hantei_ryouiki_satan / self.length_of_between) + 1 y = int(y_from_hantei_ryouiki_joutan / self.length_of_between) + 1 print("x = {}, y = {}\n".format(x, y)) self.select(x, y) self.update() super(TicTacToe, self).mousePressEvent(event) #class MainWindow(QGraphicsView): class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.initUI() def initUI(self): self.graphicsView = QGraphicsView() #scene = QGraphicsScene(self) scene = QGraphicsScene(self.graphicsView) scene.setSceneRect(0, 0, 400, 400) self.graphicsView.setScene(scene) self.tic_tac_toe = TicTacToe() scene.addItem(self.tic_tac_toe) self.kuro_button = QPushButton('黒', self) self.kuro_button.setCheckable(True) self.kuro_button.clicked[bool].connect(self.setColor) #scene.addItem(self.kuro_button) self.siro_button = QPushButton('白', self) self.siro_button.setCheckable(True) self.siro_button.clicked[bool].connect(self.setColor) #scene.addItem(self.siro_button) self.space_button = QPushButton('なし', self) self.space_button.setCheckable(True) self.space_button.clicked[bool].connect(self.setColor) #scene.addItem(self.space_button) vbox = QVBoxLayout() vbox.addWidget(self.kuro_button) vbox.addWidget(self.siro_button) vbox.addWidget(self.space_button) self.setLayout(vbox) #scene.setSceneRect(0, 0, 300, 300) scene.setSceneRect(0, 0, 500, 500) #self.setScene(scene) #self.graphicsView.setScene(scene) #self.setCacheMode(QGraphicsView.CacheBackground) mainLayout = QHBoxLayout() mainLayout.addWidget(self.graphicsView) mainLayout.addLayout(vbox) self.setLayout(mainLayout) self.setWindowTitle("Tic Tac Toe") def keyPressEvent(self, event): key = event.key() if key == Qt.Key_R: self.tic_tac_toe.reset() super(MainWindow, self).keyPressEvent(event) def setColor(self, pressed): source = self.sender() if source.text() == "黒": self.tic_tac_toe.now = self.tic_tac_toe.kuro elif source.text() == "白": self.tic_tac_toe.now = self.tic_tac_toe.siro elif source.text() == "なし": self.tic_tac_toe.now = self.tic_tac_toe.space if __name__ == '__main__': import sys app = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() sys.exit(app.exec_())

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ソース内容はほぼ確認できていませんが、以下のように修正したところ、動作しました。

Python

1class MainWindow(QGraphicsView): # QGraphicsViewに戻す 2 # 略 3 def initUI(self): 4 # 略 5 vbox.addWidget(self.space_button) 6 vbox.addWidget(self.graphicsView) # 追加 7 self.setLayout(vbox) 8 # 略 9 mainLayout = QHBoxLayout() 10 #mainLayout.addWidget(self.graphicsView) # 削除

イメージ説明

投稿2017/04/09 00:24

can110

総合スコア38233

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問