C++ 如何在重新构建布局之前销毁它Qt c++;
我在显示布局时有点问题。我有一个包含GridLayout、HBoxLayout和VBoxLayout的应用程序 HBoxLayout包含:按钮和行编辑 GridLayout包含:程序条(1-99) HBoxLayout和GridLayout放在VBoxLayout中 我的问题是,当我单击时,网格布局显示在HBoxLayout上。就像HBoxLayout不是VBoxLayout的一部分。所以,即使我破坏了布局并再次创建它,我仍然有同样的问题。 下面显示了一幅图像,以便更好地理解我的问题 代码:window.cppC++ 如何在重新构建布局之前销毁它Qt c++;,c++,qt5,C++,Qt5,我在显示布局时有点问题。我有一个包含GridLayout、HBoxLayout和VBoxLayout的应用程序 HBoxLayout包含:按钮和行编辑 GridLayout包含:程序条(1-99) HBoxLayout和GridLayout放在VBoxLayout中 我的问题是,当我单击时,网格布局显示在HBoxLayout上。就像HBoxLayout不是VBoxLayout的一部分。所以,即使我破坏了布局并再次创建它,我仍然有同样的问题。 下面显示了一幅图像,以便更好地理解我的问题 代码:
Window::Window(QWidget *parent) :
QWidget(parent){
label = NULL;
progressbar = NULL;
workerThread = NULL;
m_counter = NULL;
layout = new QHBoxLayout(NULL);
lineEdit = new QLineEdit(NULL);
m_button = new QPushButton("click", NULL);
time= new QTimer(this);
lineEdit-> setInputMask("00");;
//lineEdit->setGeometry(400,10,160,30);
lineEdit->setPlaceholderText("N between 1 & 99");
// Create and position the button
// m_button->setGeometry(100, 10, 150, 30);
QIcon icon("/home/ca/Downloads/chauvin.png");
m_button->setIcon(icon);
m_button->setIconSize(QSize(100, 30));
m_button->setToolTip("this is a beautifull button ");
//layout = new QFormLayout;
layout->addWidget(m_button);
layout->addWidget(lineEdit);
setLayout( layout );
// showTime();
time->start(1000);
setWindowTitle(tr("Digital Clock"));
// NEW : Do the connection
connect(m_button, &QPushButton::pressed, this , &Window::slotButtonPressed);}
void Window::slotButtonPressed(){
layoutgrid = new QGridLayout(NULL);
m_button->setEnabled(true);
QString contenu = lineEdit->text();
int i_contenunumber= contenu.toInt(0,10);
m_counter= new int[i_contenunumber];
memset( m_counter, 0, i_contenunumber );
label = new QLabel*[ i_contenunumber ];
progressbar = new QProgressBar*[ i_contenunumber ];
workerThread= new mythread*[ i_contenunumber ];
int x= 0;
int y= -1;
int i=0;
m_button->setText("Checked");
for(i=0;i< i_contenunumber ;i++)
{
if (i%5==0)
{
x=0;
y++;
}
int i_Randomvalue = rand() % 500 + 100;
// label[i] = new QLabel(NULL);
// //label[i]->setGeometry(100*x*1.7, 80+(50*y), 160, 30);
// label[i]->setText("Tread" + QString::number(i_Randomvalue));
// label[i]->setVisible(true);
progressbar[i] = new QProgressBar(NULL);
progressbar[i]->setRange(0, 100);
progressbar[i]->setOrientation(Qt::Horizontal);
//progressbar[i]->setGeometry(100*x*1.7,60+(50*y),150,30);
progressbar[i]->setValue(0);
progressbar[i]->setVisible(true);
layoutgrid->addWidget(progressbar[i],y,x);
setLayout(layoutgrid);
workerThread[i] = new mythread(i_Randomvalue, i);
connect(workerThread[i], &mythread::signalemit, this, &Window::barprogress);enter code here
connect(workerThread[i], &mythread::signalFinThread, this, &Window::findethread);
workerThread[i] ->start();
x++;
m_counter[i]=0;
}
// Window::setFixedSize(1000,120+(50*y-1));
removeLayout();
layoutvbox = new QVBoxLayout(NULL);
layoutvbox->addLayout(layout);
layoutvbox->addLayout(layoutgrid);
setLayout(layoutvbox);
adjustSize();
}
void Window::removeLayout ( void )
{
QLayout* po_l_layout = QWidget::layout ();
if (po_l_layout != 0)
{
QLayoutItem *item;
while ((item = po_l_layout->takeAt(0)) != 0)
po_l_layout->removeItem (item);
delete po_l_layout;
}
}
Window::Window(QWidget*父项):
QWidget(父级){
label=NULL;
progressbar=NULL;
workerThread=NULL;
m_计数器=空;
布局=新的QHBoxLayout(空);
lineEdit=新的QLineEdit(空);
m_按钮=新的QPushButton(“单击”,空);
时间=新的QTimer(本);
lineEdit->setInputMask(“00”);;
//lineEdit->setGeometry(400,10160,30);
lineEdit->设置占位符文本(“1和99之间的N”);
//创建并定位按钮
//m_按钮->设置几何体(100、10、150、30);
QIcon图标(“/home/ca/Downloads/chauvin.png”);
m_按钮->设置图标(图标);
m_按钮->设置图标大小(QSize(100,30));
m_按钮->设置工具提示(“这是一个漂亮的按钮”);
//布局=新的QFormLayout;
布局->添加小部件(m_按钮);
布局->添加小部件(lineEdit);
设置布局(布局);
//showTime();
时间->开始(1000);
setWindowTitle(tr(“数字时钟”));
//新的:做连接
连接(m_按钮,&QPushButton::pressed,this,&Window::slotButtonPressed);}
void Window::slotButtonPressed(){
layoutgrid=新QGridLayout(空);
m_按钮->设置启用(真);
QString contenu=lineEdit->text();
int i_contenunumber=contenu.toInt(0,10);
m_计数器=新整数[i_内容编号];
memset(m_计数器,0,i_内容编号);
标签=新QLabel*[i_contenunumber];
progressbar=新的QProgressBar*[i_contenunumber];
workerThread=newmythread*[i_contenunumber];
int x=0;
int y=-1;
int i=0;
m_按钮->设置文本(“选中”);
对于(i=0;isetGeometry(100*x*1.7,80+(50*y),160,30);
//标签[i]->setText(“踏面”+QString::number(i_随机值));
//标签[i]->setVisible(真);
progressbar[i]=新的QProgressBar(空);
progressbar[i]>setRange(01100);
progressbar[i]>setOrientation(Qt::水平);
//progressbar[i]->设置几何体(100*x*1.7,60+(50*y),150,30);
progressbar[i]>setValue(0);
progressbar[i]>setVisible(真);
layoutgrid->addWidget(progressbar[i],y,x);
setLayout(layoutgrid);
workerThread[i]=新的mythread(i_Randomvalue,i);
连接(workerThread[i],&mythread::signalemit,this,&Window::barprogress);在此处输入代码
连接(workerThread[i],&mythread::signalFinThread,this,&Window::FindThread);
workerThread[i]->start();
x++;
m_计数器[i]=0;
}
//窗口::设置固定大小(1000120+(50*y-1));
移除布局();
layoutvbox=新的QVBoxLayout(空);
layoutvbox->addLayout(布局);
layoutvbox->addLayout(layoutgrid);
setLayout(layoutvbox);
调整大小();
}
空窗口::移除布局(空)
{
QLayout*po_l_layout=QWidget::layout();
if(po_l_布局!=0)
{
QLayoutItem*项目;
而((项目=采购订单布局->采购订单(0))!=0)
采购订单布局->移除项目(项目);
删除po_l_布局;
}
}
设置小部件的布局后,除非删除旧布局,否则无法对其进行更改。我的建议是删除旧的布局
//remove all of layouts widgets here
delete layout
然后创建QVBoxLayout并调用setLayout(layoutvbox)
如果您希望来回切换,只需在需要时重新创建/删除即可。删除布局在此处不起作用,因为它不会删除所有包含的小部件 相反,只需创建一个包含布局的新小部件
m_widget = new QWidget();
QGridLayout *outerLayout = new QGridLayout(m_widget);
m_layout->addWidget(m_widget);
this->setLayout(m_layout);
要删除和重建,只需删除小部件
delete m_widget
删除小部件会删除小部件中包含的所有子布局和小部件这并不完全正确——您可以随时修改布局(我的程序通常会这样做)。我遇到的一个限制是,一旦您将一个小部件添加到QGridLayout的行或列中,QGridLayout中的行数(或列数)将被扩展以容纳该小部件,并且似乎没有任何方法再次减少行/列数(即使您稍后删除该小部件)@Jeremy Friesner一旦设置了布局,您可以更改布局吗?我知道你可以编辑布局(添加小部件/删除小部件)。但是,您是否可以将布局设置为完全不同的布局,而无需先删除/删除它?网格布局也很有趣,我不知道这是一种互动。
delete m_widget