Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt - forcing one tab to appear first?

Quick question - I'm working on a GUI in pyqt, and it has two tabs. Right now the second tab is always open on startup, which I think is because it runs a function to find a filename to stick in a QLineEdit. I would really like the first tab to appear on startup instead. How would I go about doing this?

like image 424
Emily C Avatar asked Feb 25 '15 02:02

Emily C


People also ask

Why use PySide instead of PyQt?

Advantages of PySide PySide represents the official set of Python bindings backed up by the Qt Company. PySide comes with a license under the LGPL, meaning it is simpler to incorporate into commercial projects when compared with PyQt. It allows the programmer to use QtQuick or QML to establish the user interface.

Is PyQt better than PySide?

PyQt is significantly older than PySide and, partially due to that, has a larger community and is usually ahead when it comes to adopting new developments. It is mainly developed by Riverbank Computing Limited and distributed under GPL v3 and a commercial license.

Is PyQt good for GUI?

PyQt is a Python binding for Qt, which is a set of C++ libraries and development tools that include platform-independent abstractions for Graphical User Interfaces (GUI), as well as networking, threads, regular expressions, SQL databases, SVG, OpenGL, XML, and many other powerful features.

How do I hide tabs in PyQt?

Since PyQT version 5.15 you can use setTabVisible(index, visible) . It will hide the tab at given index. When you will pass visible as true, it will show that tab and index will remain the same.


2 Answers

If you build your UI using Qt Creator, the tab that was active when you saved the UI is set as the default tab. You can correct this by going back into Qt Creator, selecting that tab you want to be the default and resaving it and recreating your .ui to .py file.

Alternatively, you can utilize QTabWidgets setCurrentIndex(int).

Set int equal to the index of the tab you want to display.

Example:

from PyQt4 import QtGui
from PyQt4 import QtCore
import sys

def main():   
    app     = QtGui.QApplication(sys.argv)
    tabs    = QtGui.QTabWidget()    
    tab1    = QtGui.QWidget()   
    tab2    = QtGui.QWidget()
    tab3    = QtGui.QWidget()
    tabs.addTab(tab1,"Tab 1")
    tabs.addTab(tab2,"Tab 2")
    tabs.addTab(tab3,"Tab 3")   
    tabs.setWindowTitle('PyQt QTabWidget Add Tabs and Widgets Inside Tab')
    tabs.show() 

    # This will set "Tab 2" to be shown when the application launches
    tabs.setCurrentIndex(1)   
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

This will launch a window that has "Tab 2" active.

Tab 2 is active

If the line below is removed, then "Tab 1" is active at launch

tabs.setCurrentIndex(1)
like image 121
Andy Avatar answered Oct 05 '22 23:10

Andy


The best way is to just change "1" to "0" in line:

tabs.setCurrentIndex(1)

like

tabs.setCurrentIndex(0)
like image 40
Klint Krossa Avatar answered Oct 05 '22 23:10

Klint Krossa