expanding tabs to full window size?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I've been struggling with something related to tabs for a few days now. I'm trying to make it so that the tab bar I have in my application extends across the full width of the window size. I’m working with both MacOS and Ubuntu, and while the final result of both differs, neither work.
Up to now, I've tried a few things. First, I made subclass of QtWidgets.QTabBar and use that as the tab bar. I tried setting expand to true (self.setExpand(True)), but from everything I’ve heard, that doesn’t let you override OS defaults.
The next idea was to override tabSizeHint, and it seems like this is the right approach, but I haven’t been able to figure out the implementation. Here’s what I’m doing as of now, that to me, seems like it should work:
import sys
from PySide2 import QtCore, QtWidgets, QtGui
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
self.setExpanding(True)
def tabSizeHint(self, index):
size = QtWidgets.QTabBar.tabSizeHint(self, index)
width = self.parent().size().width()
size.setWidth( width / self.count() )
return size
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.show()
sys.exit(app.exec_())
This works when I initially start the program, but does not expand the size of the tabs if I expand the window. It seems that tabSizeHint is not getting called. Another (failed) approach I took is the follow:
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.bar)
self.setLayout(layout)
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.show()
sys.exit(app.exec_())
For this one, whenever I click on the tab, it will briefly show the 4 tabs taking up the whole width (as I want it to), but in the middle of the window rather than the top. Shortly after that, it will disappear and revert back to the default sized tabs on the top of the window, as though it's being overwritten by something else.
python pyside2 qtabwidget qtabbar
add a comment |
I've been struggling with something related to tabs for a few days now. I'm trying to make it so that the tab bar I have in my application extends across the full width of the window size. I’m working with both MacOS and Ubuntu, and while the final result of both differs, neither work.
Up to now, I've tried a few things. First, I made subclass of QtWidgets.QTabBar and use that as the tab bar. I tried setting expand to true (self.setExpand(True)), but from everything I’ve heard, that doesn’t let you override OS defaults.
The next idea was to override tabSizeHint, and it seems like this is the right approach, but I haven’t been able to figure out the implementation. Here’s what I’m doing as of now, that to me, seems like it should work:
import sys
from PySide2 import QtCore, QtWidgets, QtGui
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
self.setExpanding(True)
def tabSizeHint(self, index):
size = QtWidgets.QTabBar.tabSizeHint(self, index)
width = self.parent().size().width()
size.setWidth( width / self.count() )
return size
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.show()
sys.exit(app.exec_())
This works when I initially start the program, but does not expand the size of the tabs if I expand the window. It seems that tabSizeHint is not getting called. Another (failed) approach I took is the follow:
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.bar)
self.setLayout(layout)
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.show()
sys.exit(app.exec_())
For this one, whenever I click on the tab, it will briefly show the 4 tabs taking up the whole width (as I want it to), but in the middle of the window rather than the top. Shortly after that, it will disappear and revert back to the default sized tabs on the top of the window, as though it's being overwritten by something else.
python pyside2 qtabwidget qtabbar
Updated with example
– bag531
Nov 16 '18 at 15:28
1
Yup! Worked like a charm :)
– bag531
Nov 21 '18 at 1:03
add a comment |
I've been struggling with something related to tabs for a few days now. I'm trying to make it so that the tab bar I have in my application extends across the full width of the window size. I’m working with both MacOS and Ubuntu, and while the final result of both differs, neither work.
Up to now, I've tried a few things. First, I made subclass of QtWidgets.QTabBar and use that as the tab bar. I tried setting expand to true (self.setExpand(True)), but from everything I’ve heard, that doesn’t let you override OS defaults.
The next idea was to override tabSizeHint, and it seems like this is the right approach, but I haven’t been able to figure out the implementation. Here’s what I’m doing as of now, that to me, seems like it should work:
import sys
from PySide2 import QtCore, QtWidgets, QtGui
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
self.setExpanding(True)
def tabSizeHint(self, index):
size = QtWidgets.QTabBar.tabSizeHint(self, index)
width = self.parent().size().width()
size.setWidth( width / self.count() )
return size
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.show()
sys.exit(app.exec_())
This works when I initially start the program, but does not expand the size of the tabs if I expand the window. It seems that tabSizeHint is not getting called. Another (failed) approach I took is the follow:
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.bar)
self.setLayout(layout)
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.show()
sys.exit(app.exec_())
For this one, whenever I click on the tab, it will briefly show the 4 tabs taking up the whole width (as I want it to), but in the middle of the window rather than the top. Shortly after that, it will disappear and revert back to the default sized tabs on the top of the window, as though it's being overwritten by something else.
python pyside2 qtabwidget qtabbar
I've been struggling with something related to tabs for a few days now. I'm trying to make it so that the tab bar I have in my application extends across the full width of the window size. I’m working with both MacOS and Ubuntu, and while the final result of both differs, neither work.
Up to now, I've tried a few things. First, I made subclass of QtWidgets.QTabBar and use that as the tab bar. I tried setting expand to true (self.setExpand(True)), but from everything I’ve heard, that doesn’t let you override OS defaults.
The next idea was to override tabSizeHint, and it seems like this is the right approach, but I haven’t been able to figure out the implementation. Here’s what I’m doing as of now, that to me, seems like it should work:
import sys
from PySide2 import QtCore, QtWidgets, QtGui
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
self.setExpanding(True)
def tabSizeHint(self, index):
size = QtWidgets.QTabBar.tabSizeHint(self, index)
width = self.parent().size().width()
size.setWidth( width / self.count() )
return size
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.show()
sys.exit(app.exec_())
This works when I initially start the program, but does not expand the size of the tabs if I expand the window. It seems that tabSizeHint is not getting called. Another (failed) approach I took is the follow:
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.bar)
self.setLayout(layout)
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.show()
sys.exit(app.exec_())
For this one, whenever I click on the tab, it will briefly show the 4 tabs taking up the whole width (as I want it to), but in the middle of the window rather than the top. Shortly after that, it will disappear and revert back to the default sized tabs on the top of the window, as though it's being overwritten by something else.
python pyside2 qtabwidget qtabbar
python pyside2 qtabwidget qtabbar
edited Nov 16 '18 at 18:20
eyllanesc
87.4k103564
87.4k103564
asked Nov 16 '18 at 14:40
bag531bag531
445
445
Updated with example
– bag531
Nov 16 '18 at 15:28
1
Yup! Worked like a charm :)
– bag531
Nov 21 '18 at 1:03
add a comment |
Updated with example
– bag531
Nov 16 '18 at 15:28
1
Yup! Worked like a charm :)
– bag531
Nov 21 '18 at 1:03
Updated with example
– bag531
Nov 16 '18 at 15:28
Updated with example
– bag531
Nov 16 '18 at 15:28
1
1
Yup! Worked like a charm :)
– bag531
Nov 21 '18 at 1:03
Yup! Worked like a charm :)
– bag531
Nov 21 '18 at 1:03
add a comment |
1 Answer
1
active
oldest
votes
The QTabWidget
calculates when it is necessary to change the size of the QTabBar
, so the solution is to force to have a new width that is equal to that of the QTabWidget
.
from PySide2 import QtCore, QtGui, QtWidgets
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
super(TabWindow, self).resizeEvent(event)
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.resize(640, 480)
main_app.show()
sys.exit(app.exec_())
With the previous implementation if the width is small, the buttons appear (in Linux, I have not tested in Mac OS), one way to avoid this is to implement the tabSizeHint()
method:
from PySide2 import QtCore, QtGui, QtWidgets
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
self.setExpanding(True)
def tabSizeHint(self, index):
if self.count() > 0:
size = QtWidgets.QTabBar.tabSizeHint(self, index)
width = self.parent().size().width()/self.count()
return QtCore.QSize(width, size.height())
return super(TabBar, self).tabSizeHint(index)
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
super(TabWindow, self).resizeEvent(event)
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.resize(640, 480)
main_app.show()
sys.exit(app.exec_())
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339969%2fexpanding-tabs-to-full-window-size%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The QTabWidget
calculates when it is necessary to change the size of the QTabBar
, so the solution is to force to have a new width that is equal to that of the QTabWidget
.
from PySide2 import QtCore, QtGui, QtWidgets
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
super(TabWindow, self).resizeEvent(event)
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.resize(640, 480)
main_app.show()
sys.exit(app.exec_())
With the previous implementation if the width is small, the buttons appear (in Linux, I have not tested in Mac OS), one way to avoid this is to implement the tabSizeHint()
method:
from PySide2 import QtCore, QtGui, QtWidgets
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
self.setExpanding(True)
def tabSizeHint(self, index):
if self.count() > 0:
size = QtWidgets.QTabBar.tabSizeHint(self, index)
width = self.parent().size().width()/self.count()
return QtCore.QSize(width, size.height())
return super(TabBar, self).tabSizeHint(index)
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
super(TabWindow, self).resizeEvent(event)
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.resize(640, 480)
main_app.show()
sys.exit(app.exec_())
add a comment |
The QTabWidget
calculates when it is necessary to change the size of the QTabBar
, so the solution is to force to have a new width that is equal to that of the QTabWidget
.
from PySide2 import QtCore, QtGui, QtWidgets
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
super(TabWindow, self).resizeEvent(event)
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.resize(640, 480)
main_app.show()
sys.exit(app.exec_())
With the previous implementation if the width is small, the buttons appear (in Linux, I have not tested in Mac OS), one way to avoid this is to implement the tabSizeHint()
method:
from PySide2 import QtCore, QtGui, QtWidgets
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
self.setExpanding(True)
def tabSizeHint(self, index):
if self.count() > 0:
size = QtWidgets.QTabBar.tabSizeHint(self, index)
width = self.parent().size().width()/self.count()
return QtCore.QSize(width, size.height())
return super(TabBar, self).tabSizeHint(index)
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
super(TabWindow, self).resizeEvent(event)
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.resize(640, 480)
main_app.show()
sys.exit(app.exec_())
add a comment |
The QTabWidget
calculates when it is necessary to change the size of the QTabBar
, so the solution is to force to have a new width that is equal to that of the QTabWidget
.
from PySide2 import QtCore, QtGui, QtWidgets
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
super(TabWindow, self).resizeEvent(event)
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.resize(640, 480)
main_app.show()
sys.exit(app.exec_())
With the previous implementation if the width is small, the buttons appear (in Linux, I have not tested in Mac OS), one way to avoid this is to implement the tabSizeHint()
method:
from PySide2 import QtCore, QtGui, QtWidgets
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
self.setExpanding(True)
def tabSizeHint(self, index):
if self.count() > 0:
size = QtWidgets.QTabBar.tabSizeHint(self, index)
width = self.parent().size().width()/self.count()
return QtCore.QSize(width, size.height())
return super(TabBar, self).tabSizeHint(index)
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
super(TabWindow, self).resizeEvent(event)
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.resize(640, 480)
main_app.show()
sys.exit(app.exec_())
The QTabWidget
calculates when it is necessary to change the size of the QTabBar
, so the solution is to force to have a new width that is equal to that of the QTabWidget
.
from PySide2 import QtCore, QtGui, QtWidgets
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
super(TabWindow, self).resizeEvent(event)
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.resize(640, 480)
main_app.show()
sys.exit(app.exec_())
With the previous implementation if the width is small, the buttons appear (in Linux, I have not tested in Mac OS), one way to avoid this is to implement the tabSizeHint()
method:
from PySide2 import QtCore, QtGui, QtWidgets
class TabBar(QtWidgets.QTabBar):
def __init__(self, parent=None):
super(TabBar, self).__init__(parent)
self.setExpanding(True)
def tabSizeHint(self, index):
if self.count() > 0:
size = QtWidgets.QTabBar.tabSizeHint(self, index)
width = self.parent().size().width()/self.count()
return QtCore.QSize(width, size.height())
return super(TabBar, self).tabSizeHint(index)
class TabWindow(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super(TabWindow, self).__init__(parent)
self.bar = TabBar()
self.setTabBar(self.bar)
self.initTabs()
def initTabs(self):
self.test1 = self.addTab(QtWidgets.QWidget(), "Test 1")
self.test2 = self.addTab(QtWidgets.QWidget(),"Test 2")
self.test3 = self.addTab(QtWidgets.QWidget(),"Test 3")
self.test4 = self.addTab(QtWidgets.QWidget(),"Test 4")
def resizeEvent(self, event):
self.tabBar().setFixedWidth(self.width())
super(TabWindow, self).resizeEvent(event)
class MainApplication(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)
self.mainWidget = TabWindow()
self.setCentralWidget(self.mainWidget)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
main_app = MainApplication()
main_app.resize(640, 480)
main_app.show()
sys.exit(app.exec_())
edited Nov 16 '18 at 18:29
answered Nov 16 '18 at 18:19
eyllanesceyllanesc
87.4k103564
87.4k103564
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339969%2fexpanding-tabs-to-full-window-size%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Updated with example
– bag531
Nov 16 '18 at 15:28
1
Yup! Worked like a charm :)
– bag531
Nov 21 '18 at 1:03