logo
down
shadow

How to Pack with PyQt - how to make QFrame/Layout adapt to content


How to Pack with PyQt - how to make QFrame/Layout adapt to content

By : Ivan Bautrukevich
Date : January 12 2021, 08:33 AM
this one helps. To get the right size, you must use the idealWidth() method of document(), but this will only be correct when the item is visible so you should apply it when the widget is visible, so you should call the show() method that this change is applied, in addition to calling it every time the text changes.
To get the correct height, use the size() method of QTextDocument()
code :
class Box(QTextBrowser):
    def __init__(self, parent=None):
        QTextBrowser.__init__(self, parent)
        self.setContentsMargins(0, 0, 0, 0)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        cstring = """
        QTextBrowser {
            border: 0;
            background-color: #<---->;
            margin: 0px;
            padding-left:0;
            padding-top:0;
            padding-bottom:0;
            padding-right:0;
        }
        """

        ncol = randint(300000, 999999)
        cstring = cstring.replace('<---->', str(ncol))
        self.setStyleSheet(cstring)

        self.document().contentsChange.connect(lambda: self.customGeometry())
        self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
        self.setContentsMargins(0, 0, 0, 0)

    def customGeometry(self):
        if self.isVisible():
            self.setFixedWidth(self.document().idealWidth())
            self.setFixedHeight(self.document().size().height())

    def showEvent(self, event):
        self.customGeometry()
        QTextBrowser.showEvent(self, event)


class MainFrame(QScrollArea):
    def __init__(self, parent=None):
        QScrollArea.__init__(self, parent)
        container = QFrame(self)

        layout = QGridLayout(container)
        layout.setSpacing(0)
        layout.setContentsMargins(0, 0, 0, 0)
        self.show()

        text = '''
                <table border="0" cellspacing="0" cellpadding="5" style="background-color: rgba(119, 212, 212, 0.7);">
                <tr>
                    <td width="100">
                        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
                    </td>
                <tr>
                <tr>
                   <td>
                        bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
                    </td>
                <tr>
                </table>
                '''

        for row in range(5):
            for col in range(10):
                box = Box(container)
                box.setText(text)
                box.show()
                layout.addWidget(box, row, col)

        self.setWidget(container)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    ex = MainFrame()
    ex.show()
    sys.exit(app.exec_())


Share : facebook icon twitter icon
How to delete QFrame in pyqt

How to delete QFrame in pyqt


By : Alfio
Date : March 29 2020, 07:55 AM
around this issue Deleting or removing the other frames is probably the wrong approach.
It's much more efficient to simply hide/show the frames as needed. There are several ways to achieve this, but a QStackedLayout sounds like the best fit for your puposes. All you need to do is add each set of frames as a separate layer/page in the stack, then use setCurrentIndex to bring the required item to the top. A stacked-layout is just like a tab-widget, but without the tab-bar and outer frame.
code :
from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        def make_frame(text, parent):
            frame = QtGui.QFrame(parent)
            frame.setFrameStyle(QtGui.QFrame.StyledPanel)
            layout = QtGui.QHBoxLayout(frame)
            label = QtGui.QLabel(text, frame)
            label.setAlignment(QtCore.Qt.AlignCenter)
            layout.addWidget(label)
            return frame
        self.splitter = QtGui.QSplitter(self)
        for text in 'ONE TWO THREE'.split():
            self.splitter.addWidget(make_frame(text, self.splitter))
        self.stack = QtGui.QStackedLayout()
        self.stack.addWidget(self.splitter)
        self.stack.addWidget(make_frame('FOUR', self))
        self.button = QtGui.QPushButton('Switch', self)
        self.button.clicked.connect(
            lambda: self.stack.setCurrentIndex(
                int(self.stack.currentIndex() == 0)))
        layout = QtGui.QVBoxLayout(self)
        layout.addLayout(self.stack)
        layout.addWidget(self.button)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 800, 500)
    window.show()
    sys.exit(app.exec_())
How the layout adapt to content horizontally and vertically?

How the layout adapt to content horizontally and vertically?


By : user3408689
Date : March 29 2020, 07:55 AM
wish helps you Think about GridView, Refer to Android Developer
You should define grid view container in your_layout.xml
code :
<your.package.ButtonsGridView
android:id="@+id/buttonsGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
ButtonsGridView buttonsGridView = (ButtonsGridView) findViewById(R.id.buttonsGridView);
ButtonsAdapter buttonsAdapter = new ButtonsAdapter(Context);
buttonsGridView.setAdapter(buttonsAdapter);
Trouble Getting UITextViews to Adapt to content Using Auto-Layout while Embedded in UIScrollView

Trouble Getting UITextViews to Adapt to content Using Auto-Layout while Embedded in UIScrollView


By : user3434738
Date : March 29 2020, 07:55 AM
should help you out A better way to do this would be to use a static UITableView.
I showed an example of how to do this with code and screenshots on my blog.
How to make our layout adapt with our screen devices in android?

How to make our layout adapt with our screen devices in android?


By : jif86
Date : March 29 2020, 07:55 AM
Hope that helps Firstly i strongly recommended you to go through the android developer docs for How to support multiple screen in Android and Supporting Different Screen Sizes
Coming to your question, For Different screen size, The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for small, medium, high, and extra high density screens. you could use different size of the layout files in res folder and also vary for drawable images based on the density..
code :
  res/layout/my_layout.xml             // layout for normal screen size ("default")
  res/layout-small/my_layout.xml       // layout for small screen size
  res/layout-large/my_layout.xml       // layout for large screen size
  res/layout-xlarge/my_layout.xml      // layout for extra large screen size
  res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
Adding a Qframe in PYQT and set size

Adding a Qframe in PYQT and set size


By : breuerio
Date : March 29 2020, 07:55 AM
wish of those help You created a frame but never add it to any layout, so it doesn't show.
QMainWindow comes with a predefined layout with a menu bar, toolbar, status bar etc (Qt Doc). To show the frame you could just do self.setCentralWidget(self.frame), it would be inserted in the main window layout.
code :
class gameWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.setGeometry(300,300,1280,800)

        self.frame = QtGui.QFrame()
        self.frame.resize(300,300)
        self.frame.setStyleSheet("background-color: rgb(200, 255, 255)")

        layout=QtGui.QVBoxLayout()
        layout.addWidget(self.frame)
        self.setLayout(layout)
Related Posts Related Posts :
  • How to calculate each single element of a numpy array based on conditions
  • How do I change the width of Jupyter notebook's cell's left part?
  • Measure distance between lat/lon coordinates and utm coordinates
  • Installing megam for NLTK on Windows
  • filter dataframe on each value of a samn column have a specific value of another column in Panda\Python
  • Threading with pubsub throwing AssertionError: 'callableObj is not callable' in wxPython
  • Get grouped data from 2 dataframes with condition
  • How can I import all of sklearns regressors
  • How to take all elements except the first k
  • Whats wrong with my iteration list of lists from csv
  • Tensorflow Estimator API save image summary in eval mode
  • How do I get certain Time Range in Python
  • python doubly linked list - insertAfter node
  • Open .h5 file in Python
  • Joining a directory name with a binary file name
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • Pandas concat historical data using date minus some number of days
  • CV2: Import Error in Python OpenCV
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org