matman9
matman9

Reputation: 490

Move PyQt dialog box to tab

I have a GUI that I'm trying to make a tabbed window interface with different functionalities; load data, clean data, process data etc.

I have a button which enables selection of a category from a list. I can't figure out how to it move to my tab. So when I change tab, the dialog is always on top, when i'd like for it to be on the first tab.

I can move the "Select category" button like the other buttons and add a label above to dispaly the selection, but can't figure out how to move the dialog box with the addRow layout. Is this possible?

Thanks.

Code below;

from __future__ import division, print_function
import sys
from PyQt4 import QtGui, QtCore


class tester(QtGui.QWidget):

    def __init__(self):
        # create GUI
        QtGui.QMainWindow.__init__(self)
        self.setWindowTitle('Data tool')

        # window dimensions
        self.resize(300, 75)
        form = QtGui.QFormLayout()

        # tabbing on
        tab_widget = QtGui.QTabWidget()
        tab1 = QtGui.QWidget()
        tab2 = QtGui.QWidget()
        self.p1_vertical = QtGui.QVBoxLayout(tab1)
        self.p2_vertical = QtGui.QVBoxLayout(tab2)
        tab_widget.addTab(tab1, "Data selection")
        tab_widget.addTab(tab2, "Tools")

        # selected dir label
        self.lbl_dir = QtGui.QLabel('Data directory not selected')
        self.p1_vertical.addWidget(self.lbl_dir)

        # button label for datadir
        btn_dir = QtGui.QPushButton('Browse...', self)
        self.p1_vertical.addWidget(btn_dir)
        self.connect(btn_dir, QtCore.SIGNAL('clicked()'), self.get_dirname)

        # this is the bit i want to be in the tab
        # get category input
        self.btn_cat = QtGui.QPushButton("Select category")
        self.btn_cat.clicked.connect(self.getcat)
        self.le3 = QtGui.QLineEdit()
        form.addRow(self.btn_cat, self.le3)
        self.le3.setReadOnly(True)

        # label for status of run
        self.lblrun = QtGui.QLabel('Run')
        self.p2_vertical.addWidget(self.lblrun)

        # button for run
        btn_run = QtGui.QPushButton('Run', self)
        self.p2_vertical.addWidget(btn_run)
        self.connect(btn_run, QtCore.SIGNAL('clicked()'), self.run)

        # vertical layout for widgets
        self.vbox = QtGui.QVBoxLayout()
        self.vbox.addLayout(form)
        self.vbox.addWidget(tab_widget)
        self.setLayout(self.vbox)

    def getcat(self):
        cats = ['option 1', 'option 2'] 
        category, ok = QtGui.QInputDialog.getItem(self, "select category", 
            "Option", cats, 0, False)
        if ok:
            self.le3.setText(category)
            self.category = category

    def get_dirname(self):
        print('yes')

    def run(self):
        print('yes')

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    gui = tester()
    gui.show()
    app.exec_()

Looks like this;

test GUI

Would like to be like this;

enter image description here

Upvotes: 1

Views: 539

Answers (1)

eyllanesc
eyllanesc

Reputation: 244142

You have to set the QFormLayout in the layout of tab1 to eliminate the following:

self.p1_vertical.addLayout(form)

and adds:

self.vbox.addLayout(form)

Although for a better organization I prefer to create new classes:

from __future__ import division, print_function
import sys
from PyQt4 import QtGui, QtCore

class DataSelectionWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(DataSelectionWidget, self).__init__(parent)

        self.btn_cat = QtGui.QPushButton("Select category")
        self.btn_cat.clicked.connect(self.getcat)
        self.le3 = QtGui.QLineEdit(readOnly=True)
        flay = QtGui.QFormLayout(self)
        flay.addRow(self.btn_cat, self.le3)

        # selected dir label
        self.lbl_dir = QtGui.QLabel('Data directory not selected')
        flay.addRow(self.lbl_dir)

        # button label for datadir
        btn_dir = QtGui.QPushButton('Browse...', self)
        flay.addRow(btn_dir)
        btn_dir.clicked.connect(self.get_dirname)

    @QtCore.pyqtSlot()
    def getcat(self):
        cats = ['option 1', 'option 2'] 
        category, ok = QtGui.QInputDialog.getItem(self, "select category", 
            "Option", cats, 0, False)
        if ok:
            self.le3.setText(category)
            self.category = category        

    @QtCore.pyqtSlot()
    def get_dirname(self):
        print('yes')


class ToolsWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(ToolsWidget, self).__init__(parent)
        lay = QtGui.QVBoxLayout(self)

        self.lblrun = QtGui.QLabel('Run')
        lay.addWidget(self.lblrun)

        # button for run
        btn_run = QtGui.QPushButton('Run', self)
        lay.addWidget(btn_run)
        btn_run.clicked.connect(self.run)

    @QtCore.pyqtSlot()
    def run(self):
        print('yes')


class Tester(QtGui.QWidget):
    def __init__(self, parent=None):
        # create GUI
        super(Tester, self).__init__(parent)
        self.setWindowTitle('Data tool')
        # window dimensions
        self.resize(300, 75)
        tab1 = DataSelectionWidget()
        tab2 = ToolsWidget()
        # tabbing on
        tab_widget = QtGui.QTabWidget()
        tab_widget.addTab(tab1, "Data selection")
        tab_widget.addTab(tab2, "Tools")

        lay = QtGui.QVBoxLayout(self)
        lay.addWidget(tab_widgetn)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    gui = Tester()
    gui.show()
    sys.exit(app.exec_())

Upvotes: 2

Related Questions