I am new to here.
The ui run well ,but when I click 'okbtn' ...
QObject::connect: No such slot QWidget::makeyourbox() in occQt.cpp:324
And when I click 'cancelbtn', it runs.
Thanks for any responses,
Eason
code:
void occQt::about2() //UI
{
QWidget* pWidget = new QWidget;
QLabel* longlabel = new QLabel(tr("long"));
QLabel* widthlabel = new QLabel(tr("width"));
QLabel* highlabel = new QLabel(tr("high"));
longlineedit = new QLineEdit;
widthlineedit = new QLineEdit;
highlineedit = new QLineEdit;
QPushButton* okbtn = new QPushButton(tr("ok"));
QPushButton* cancelbtn = new QPushButton(tr("cancel"));
QGridLayout* gridlayout = new QGridLayout;
QVBoxLayout* dlglayout = new QVBoxLayout;
QHBoxLayout* btnlayout = new QHBoxLayout;
gridlayout->addWidget(longlabel, 0, 0, 1, 1);
gridlayout->addWidget(widthlabel, 1, 0, 1, 1);
gridlayout->addWidget(highlabel, 2, 0, 1, 1);
gridlayout->addWidget(longlineedit, 0, 1, 1, 3);
gridlayout->addWidget(widthlineedit, 1, 1, 1, 3);
gridlayout->addWidget(highlineedit, 2, 1, 1, 3);
longlineedit->setText("5");
widthlineedit->setText("5");
highlineedit->setText("5");
btnlayout->setSpacing(60);
btnlayout->addWidget(okbtn);
btnlayout->addWidget(cancelbtn);
//pWidget->setLayout(gridlayout);
dlglayout->setMargin(50);
dlglayout->addLayout(gridlayout);
dlglayout->addStretch(40);
dlglayout->addLayout(btnlayout);
pWidget->setLayout(dlglayout);
pWidget->setWindowTitle(tr("Make a Box by custom."));
pWidget->show();
connect(okbtn, SIGNAL(clicked()), pWidget, SLOT(makeyourbox()));
//QObject::connect(okbtn, SIGNAL(clicked()), pWidget, SLOT(close()));
connect(cancelbtn, SIGNAL(clicked()), pWidget, SLOT(close()));
}
void occQt::makeyourbox()
{
QString string_a = longlineedit->text();
eason_a = string_a.toInt();
QString string_b = widthlineedit->text();
eason_b = string_b.toInt();
QString string_c = highlineedit->text();
eason_c = string_c.toInt();
TopoDS_Shape aTopoBox = BRepPrimAPI_MakeBox(eason_a, eason_b, eason_c).Shape();
Handle_AIS_Shape anAisBox = new AIS_Shape(aTopoBox);
anAisBox->SetColor(Quantity_NOC_AZURE);
mContext->Display(anAisBox);
}
When I run the pWidget, click cancelbtn, ui close.
Click okbtn,do nothing..
pWidget is a generic QWidget. It does not contain method/slot makeyourbox().
Your code is faulty.
you should add makeyourbox() method to the subclass of QWidget, and mark it as slot
Double check makeyourbox is defined to be a slot within that class.
Related
I want to add a QLineEdit to a QTabWidget but I always get a
Segmentation fault, SIGSEGV
I did it the following way:
QHBoxLayout *layout = new QHBoxLayout;
QWidget *Tab = new QWidget(ui->tabWidget);
ui->tabWidget->addTab(Tab, "Tab1");
Tab->setLayout(layout);
QLineEdit *lE = new QLineEdit();
lE->setObjectName("Text");
lE->setText("Hello");
ui->tabWidget->widget(0)->layout()->addWidget(lE);
This way works for adding a QPushButton but somehow it doesn't work for a QLineEdit.
you can add a widget to a cell
example:
//
QWidget* pWidget = new QWidget();
QLineEdit* foo = new QLineEdit(this);
foo->setText("Edit");
QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(foo);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0, 0, 0, 0);
pWidget->setLayout(pLayout);
this->ui->myTable->setCellWidget(1, 1, pWidget);
I am creating a QFormLayout with some items like this:
QFormLayout *tableLayout = new QFormLayout();
QLineEdit *line1 = new QLineEdit();
QLineEdit *line2 = new QLineEdit();
tableLayout->addRow(tr("LineText1 "), line1);
tableLayout->addRow(tr("LineText2 "), line2);
After that I try to add this Layout to a QGridLayout like this:
QGridLayout *layout = new QGridLayout();
QPushButton *btn1 = new QPushButton();
QPushButton *btn2 = new QPushButton();
layout->addWidget(btn, 1, 1, 3, 3);
layout->addWidget(btn2, 1, 4);
layout->addLayout(tableLayout, 2, 4);
After I added the tableLayout, btn1 as width as 1 column and the tableLayout is as width as 3 columns.
I already tried to put the QFormLayout into a own widget and add the widget to the QGridLayout. But it didn't changed anything. The way I am doing that is the following:
QFormLayout *tableLayout = new QFormLayout();
QLineEdit *line1 = new QLineEdit();
QLineEdit *line2 = new QLineEdit();
tableLayout->addRow(tr("LineText1 "), line1);
tableLayout->addRow(tr("LineText2 "), line2);
QWidget *widget = new QWidget();
widget->setLayout(tableLayout);
QGridLayout *layout = new QGridLayout();
QPushButton *btn1 = new QPushButton();
btn1->setText("btn1");
QPushButton *btn2 = new QPushButton();
btn2->setText("btn2");
layout->addWidget(btn1, 1, 1, 3, 3);
layout->addWidget(btn2, 1, 4);
layout->addWidget(widget, 2, 4);
What is the reason for this strange situation? And how to solve it?
Here is a picture of the result:
And here is wat I want to have:
To build the design you want the first thing is to establish the position of the elements, remember that the position of the rows or columns start at 0, not at 1 as you do. The second part is to set the size policies, some widgets already have some established policy such as the QPushButton that stretches horizontally but not vertically so even if the rowSpan is large it will not change the height of the button, so we must change that behavior and finally the stretch.
#include <QApplication>
#include <QFormLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QSizePolicy>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QGridLayout *layout = new QGridLayout(&w);
QPushButton *btn1 = new QPushButton("Btn1");
btn1->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
QPushButton *btn2 = new QPushButton("Btn2");
btn2->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
QFormLayout *tableLayout = new QFormLayout();
QLineEdit *line1 = new QLineEdit();
QLineEdit *line2 = new QLineEdit();
tableLayout->addRow("LineText1 ", line1);
tableLayout->addRow("LineText2 ", line2);
layout->addWidget(btn1, 0, 0, 3, 3);
layout->addWidget(btn2, 0, 3);
layout->addLayout(tableLayout, 1, 3);
// column 0 x3
layout->setColumnStretch(0, 3);
// column 3 x1
layout->setColumnStretch(3, 1);
w.resize(640, 480);
w.show();
return a.exec();
}
Note that the QFormLayout will make the widgets always on top, so it will not necessarily occupy the height of the space offered by the QGridLayout.
I want to do serial communication in the QT inside, according to the number of serial ports to dynamically generate label, LineEdit, Button, and these three buttons can pull down the scroll bar when the size of the interface, how to do well, I write this below is dead of.
The effect of encapsulation into a method
The interface was washed last
Define QGridLayout insude QScrollArea. And add your new widgets into
that layout in code. QGridLayout::addWidget
Define table where you will show several widgets in table cells. That real complicated way.
void BaseUi::BaseScrollArea()
{
QScrollArea *pArea = new QScrollArea();
QWidget *pWidget = new QWidget();
pWidget->setStyleSheet("QWidget" "{background:white;}");
m_vbox_layout = new QVBoxLayout();
m_vbox_layout->addSpacerItem(new QSpacerItem(100, 30,
QSizePolicy::Expanding, QSizePolicy::Expanding));
pWidget->setLayout(m_vbox_layout);
pArea->setWidget(pWidget);
pArea->setWidgetResizable(true);
m_main_layout = new QVBoxLayout();
m_main_layout->addWidget(pArea);
}
void BaseUi::addAutoRecordUi(QString lab_neme, QString ledit_name)
{
QWidget *page = new QWidget;
QGridLayout *layout = new QGridLayout(page);
QLabel *label = new QLabel;
label->setText(lab_neme);
label->setFont(font());
QLineEdit *ledit = new QLineEdit;
ledit->setText(ledit_name);
ledit->setFont(font());
layout->addWidget(label, 0, 1);
layout->addWidget(ledit, 0, 2);
page->setLayout(layout);
m_vbox_layout->insertWidget(m_vbox_layout->count()-1, page);
}
void BaseUi::addMulRecordUi(QString lab_neme, QString ledit_name, QString
but_name)
{
QWidget *page = new QWidget;
QGridLayout *layout = new QGridLayout(page);
QLabel *label = new QLabel;
label->setText(lab_neme);
label->setFont(font());
QLineEdit *ledit = new QLineEdit;
ledit->setText(ledit_name);
ledit->setFont(font());
QPushButton *but = new QPushButton(but_name);
but->setFont(font());
layout->addWidget(label, 0, 1);
layout->addWidget(ledit, 0, 2);
layout->addWidget(but, 0, 3);
page->setLayout(layout);
m_vbox_layout->insertWidget(m_vbox_layout->count()-1, page);
}
My C++ code(use opencv and qt):
face_detect::face_detect(QWidget *parent)
: QWidget(parent)
{
imgwidget = new showwidget("D:\\sample.avi");
imgwidget->resize(QSize(640,480));
setWindowTitle(tr("Face Detection!"));
FaceNumLabel = new QLabel(tr("face_num: "));
num = new QLabel;
num->setFrameStyle(QFrame::Panel | QFrame::Sunken);
ImgSource = new QLabel(tr("image source: "));
VedioBtn = new QPushButton(tr("from vedio"));
LocalImgBtn = new QPushButton(tr("local image"));
LeftLayout = new QVBoxLayout();
LeftLayout->addWidget(imgwidget);
LeftLayout->addWidget(FaceNumLabel);
LeftLayout->addWidget(num);
//LeftLayout->addWidget(ShowImageLabel, 3, 0, 1, 2);
RightLayout = new QVBoxLayout();
RightLayout->addWidget(ImgSource);
RightLayout->addWidget(VedioBtn);
RightLayout->addWidget(LocalImgBtn);
RightLayout->addWidget(ImgSource);
QHBoxLayout *mainLayout = new QHBoxLayout(this);
mainLayout->addLayout(LeftLayout);
mainLayout->addLayout(RightLayout);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
connect(VedioBtn, SIGNAL(clicked()), this, SLOT(openvedio()));
connect(LocalImgBtn, SIGNAL(clicked()), this, SLOT(localimg()));
}
I call widget.resize() to resize the widget, and then add it to the layout. But when I run the program, the showimgwidget's is not 640*480. Why is that?
for(int i=0; i<page.size(); i++){
User user= Poco::AnyCast<User>(*it);
ui.table->setItem(i,0,new QTableWidgetItem(user.userName));
ui.table->setItem(i,1,new QTableWidgetItem(user.sex));
ui.table->setItem(i,2,new QTableWidgetItem(user.age));
QPushButton* btn_edit = new QPushButton();
btn_edit = new QPushButton();
btn_edit->setText("Edit");
ui.table->setCellWidget(i,3,(QWidget*)btn_edit);
++it;
}
I add a QPushButton into the cell with the function setCellWidget(),
I know, if it's a QTableWidgetItem, I can use :
ui.table->item(0,3)->setTextAlignment(QT::AlignHCenter)
But it is a Widget,
QTableWidgetItem item = ui.table->item(0,3);
the item is null.
I can get the cell by use
ui.table->cellWidget(0,3).
How should I do to make the button centered in the cell?
Try this:
QWidget* pWidget = new QWidget();
QPushButton* btn_edit = new QPushButton();
btn_edit->setText("Edit");
QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(btn_edit);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0, 0, 0, 0);
pWidget->setLayout(pLayout);
ui.table->setCellWidget(i, 3, pWidget);