2016-11-14 35 views
-1

我有一個QVBox佈局,其中包含一個QVBox佈局和一個QHBox佈局。我使用其他QVBox佈局來保存動態創建的GUI對象,並使用QHBox佈局來保存添加/刪除這些對象的按鈕。如果我將QHBox放置在QVBox的頂部,但一切正常,但當我嘗試將QHBox放置在QVBox下方時,對象不能正確移除,但會停留在QHBox頂部的「延遲」處。我會上傳照片來展示問題。第一張照片是在採取行動之前,第二是創建一個新的對象之後,三是刪除對象Pyqt小部件被刪除後不知所云

Before action

After creating new component

After deleting the component

這裏經過的是,創建和刪除新的代碼物體

def addClient(self): 
      if (len(self.clients) < 5): 
        client = clientComponent(self) 
        self.clients.append(client) 
        index = len(self.clients)-1 
        self.vLayout3.addWidget(self.clients[index]) 

        client.setIndex(index) 

        self.clients[index].startButton.clicked.connect(partial(self.threadcontrol, '2', client.getIndex())) 
        self.clients[index].stopButton.clicked.connect(partial(self.clientstop, '0', client.getIndex())) 

    def deleteClient(self): 
      if (len(self.clients) > 1): 
        self.vLayout3.removeWidget(self.clients.pop()) 

這是我在那裏完成佈局

def initializeUi(self): 

      self.mainWidget = QWidget(self) 
      self.setCentralWidget(self.mainWidget) 

      self.mainLayout = QVBoxLayout(self.mainWidget) 
      self.hLayout1 = QHBoxLayout() 
      self.hLayout2 = QHBoxLayout() 
      self.vLayout1 = QVBoxLayout() 
      self.vLayout2 = QVBoxLayout() 
      self.vLayout3 = QVBoxLayout() 

      self.addServer() 
      self.addClient() 

      self.serverBox = QGroupBox('Server') 
      self.clientBox = QGroupBox('Client') 

      self.traffic1 = QLabel('0.0Mb/s', self) 
      self.traffic1.setAlignment(Qt.AlignRight) 
      self.traffic2 = QLabel('0.0Mb/s', self) 
      self.traffic2.setAlignment(Qt.AlignCenter) 
      self.traffic3 = QLabel('0.0Mb/s', self) 
      self.traffic3.setAlignment(Qt.AlignLeft) 

      self.newClientButton = QPushButton('+', self) 
      self.deleteClientButton = QPushButton('-', self) 

      self.hLayout1.addWidget(self.traffic1) 
      self.hLayout1.addWidget(self.traffic2) 
      self.hLayout1.addWidget(self.traffic3) 
      self.hLayout2.addWidget(self.newClientButton) 
      self.hLayout2.addWidget(self.deleteClientButton) 
      self.vLayout2.addLayout(self.vLayout3) 
      self.vLayout2.addLayout(self.hLayout2) 

      self.mainLayout.addWidget(self.plot) 
      self.mainLayout.addLayout(self.hLayout1) 
      self.serverBox.setLayout(self.vLayout1) 
      self.mainLayout.addWidget(self.serverBox) 
      self.clientBox.setLayout(self.vLayout2) 
      self.mainLayout.addWidget(self.clientBox) 
+0

要分析你的代碼,我需要你展現完整的代碼 – eyllanesc

回答

1

這是發生在主窗口,因爲你的主窗口仍然是將它們從佈局中移除後,客戶端小部件的父級。如果您將小部件指定爲父部件而不將其添加到任何佈局,則會看到類似的行爲。

刪除父項應解決問題。

def deleteClient(self): 
    if (len(self.clients) > 1): 
     client = self.clients.pop() 
     self.vLayout3.removeWidget(client) 
     client.setParent(None) 

您可能還需要做出adjustSize調用調整窗口大小以適應其餘部件

1

當你刪除佈局控件它仍然是在父控件的 object tree,所以它得到任何佈局之外的顯示。 要從對象樹中刪除一個窗口部件,請致電widget.setParent(None)widget.deleteLater()也有效。
這裏是我的MCVE(Qt4的,Py2.7):

from PyQt4.QtGui import (QApplication, QWidget, QPushButton, 
         QVBoxLayout, QHBoxLayout) 

app=QApplication([]) 

self = QWidget() 
main_layout = QVBoxLayout(self) 

clients = [] 
l2 = QHBoxLayout() 
main_layout.addLayout(l2) 

b_add = QPushButton('add', self) 
l2.addWidget(b_add) 

def addClient(): 
    b = QPushButton(str(len(clients)), self) 
    clients.append(b) 
    main_layout.addWidget(b) 
b_add.clicked.connect(addClient) 

b_rm = QPushButton('rm', self) 
l2.addWidget(b_rm) 

def deleteClient(): 
    b = clients.pop() 
    main_layout.removeWidget(b) 
    # comment out two following lines to get the behavior you observe 
    b.setParent(None) 
    self.adjustSize() 
b_rm.clicked.connect(deleteClient) 


self.show() 
app.exec_() 

在我的系統我也有叫self.adjustSize()刪除後重新調整

相關問題