I was going through a code.
void GZMainWindow::connectMarkers_cx()
{
//![1]
// Connect all markers to handler
const auto markers = cx_chart->legend()->markers();
for (QLegendMarker *marker : markers)
{
QObject::disconnect(marker, &QLegendMarker::clicked,
this, &GZMainWindow::handleMarkerClicked);
QObject::connect(marker, &QLegendMarker::clicked, this, &GZMainWindow::handleMarkerClicked);
}
//![1]
}
What does //![1] mean? Is it used for segmentation of code or function ?
Related
I have an overloaded QTreeWidget class, with my SIGNALS: I have promoted it in my UI and when I listen promoted QTreeWidget object with a lambda syntax I have an error.
QObject::connect: signal not found in CustomTreeWidget.
MY CustomTreeWidget looks like:
.h
class CustomTreeWidget : public QTreeWidget
{
Q_OBJECT
public:
explicit CustomTreeWidget(QWidget *parent = 0);
~CustomTreeWidget() {
}
signals:
void currentNodeChanged(QSet<int> uids);
void deleteRequest(QVector<int> uids);
}
.cpp
CustomTreeWidget::CustomTreeWidget(QWidget *parent) : QTreeWidget(parent)
{
setAnimated(true);
connect(this, &CustomTreeWidget::customContextMenuRequested, this, [=](const QPoint &pos) {
this->m_bCustomMenuOpen = true;
const auto &&item = this->itemAt(pos);
QMenu myMenu;
bool ok = !(item) ? false : true;
if (ok) {
//თუ topLevelItem -ია მხოლოდ დამატების action -ი უნდა იყოს ჩართული.
if (item == this->topLevelItem(0) || item == this->topLevelItem(0)->child(0)) {
ok = false;
}
}
QAction *Removecnt = myMenu.addAction(tr("&წაშლა"), this, SLOT(DeleteNode()));
Removecnt->setIcon(QIcon(":/global_res/delete.png"));
Removecnt->setEnabled(ok);
myMenu.exec(this->mapToGlobal(pos));
});
}
void CustomTreeWidget::BFS(QTreeWidgetItem *item, QSet<int> &out)
{
std::queue<QTreeWidgetItem *> Q;
Q.push(item);
while (!Q.empty()) {
QTreeWidgetItem *now = Q.front(); Q.pop();
out.insert(this->m_mapUids[now]);
for (int i = 0; i < now->childCount(); i++) {
Q.push(now->child(i));
}
}
}
QSet<int> CustomTreeWidget::GetCurrentNodeUids()
{
QSet<int> uids;
if (!this->currentItem())
return uids;
this->BFS(this->currentItem(), uids);
return uids;
}
void CustomTreeWidget::DeleteNode()
{
QSet<int> nodes = this->GetCurrentNodeUids();
QVector<int> uids;
for (auto it : nodes) {
uids.push_back(it);
}
emit deleteRequest(uids);
}
My lambda looks like:
connect(ui->productTree, &CustomTreeWidget::deleteRequest, this, [=](QVector<int> uids) {
//logic
});
But this signal works with old syntax.
connect(ui->productTree, SIGNAL(deleteRequest(QVector<int>)), this, SLOT(checkSlot(QVector<int>)));
And this slot is.
void ProductForm::checkSlot(QVector<int> uids)
{
qDebug() << uids.size();
}
So what is problem lambda syntax?
This smells like the violation of the one definition rule (ODR) - perhaps due to a stale build folder. The problem is that the address of deleteRequest passed to the connect method is not the same as the address of the method visible from moc_CustomTreeWidget.cpp. Remove the build folder and try again. If it still doesn't work, start reducing your problem:
Create a minimization branch in the repository (if you're not using version control, you won't go anywhere with minimization).
Copy-paste the contents of the ui_CustomTreeWidget.h file into the CustomTreeWidget.cpp file, remove the #include line.
Inspect the pasted contents for instantiation of the productTree object with the correct type.
If that's correct, then remove everything else from the code, step-by-step, rebuilding and committing to the repository at each step that still reproduces. You should end up with a test case that is 20-30 lines long at most. And it'll be either obvious what's wrong, or you can modify the question with the test case.
In the code above uitablewidget does not update using signal and slot.
It seems as if (ui->tableWidget->setItem(0,0,newItemx);) doesn't work.
Am I doing something wrong or is there a better way to update my qtablewidget from my class B?
Class_A::Class_A(QWidget *parent):QDialog(parent),ui(new Ui::Class_A)
{
ui->setupUi(this);
}
Class_A::~Class_A()
{
delete ui;
}
void Class_A::change_TableWidget(double x,double y) // this is the public slot
{
QTableWidgetItem *newItemx = new QTableWidgetItem(QString::number(x));
ui->tableWidget->setItem(0,0,newItemx);
QTableWidgetItem *newItemy = new QTableWidgetItem(QString::number(y));
ui->tableWidget->setItem(0,0,newItemy);
}
Class_B::Class_B(QWidget *parent) :
QGLWidget(parent)
{
Class_A *t=new Class_A;
connect(this,SIGNAL(mySignal(double,double)),t,SLOT(change_TableWidget(double,double)));
}
void Class_B::mousePressEvent(QMouseEvent *event)
{
double x = event->x();
double y = event->y();
emit mySignal(x,y);
}
You don't have a SLOT(change_TableWidget(double,double)) - yours takes 3 doubles, not two.
You should check that connect() returned true. I like to write
if (!connect(....)) Q_ASSERT(false);
or if (!connect(....)) Q_ASSERT(!"connect");
Also, connect prints out messages to the debug output when it fails to match the signals and slots. You should look for that output.
(Or use the new Qt 5 connect(), which is all checked at compile time.)
I am trying to undo/redo in multi document interface. I have different entities. Each entity has its own class. I have used UndoGroup but when I unable to push them to undoStack dont know whats's wrong there. Can anyone help me to solve the issue.
cadgraphicscene.cpp
CadGraphicsView::CadGraphicsView()
{
undoStack = new QUndoStack(this);
}
QUndoStack *CadGraphicsView::m_undoStack() const
{
return undoStack;
}
void CadGraphicsView::showUndoStack()
{
undoView = 0;
// shows the undoStack window
if (undoView == 0)
{
undoView = new QUndoView(undoStack);
undoView->setWindowTitle("Undo Stack");
}
undoView->show();
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
m_undoGroup = new QUndoGroup(this);
QAction *undoAction = m_undoGroup->createUndoAction(this);
undoAction->setShortcut(QKeySequence::Undo);
QAction *redoAction = m_undoGroup->createRedoAction(this);
redoAction->setShortcut(QKeySequence::Redo);
menuEdit->insertAction(menuEdit->actions().at(1), undoAction);
menuEdit->insertAction(undoAction, redoAction);
menuEdit->addAction(undoAction);
menuEdit->addAction(redoAction);
undoAction->setEnabled(true);
redoAction->setEnabled(true);
}
void MainWindow::updateActions()
{
CadGraphicsView *view = currentDocument();
m_undoGroup->setActiveStack(view == 0 ? 0 : view->m_undoStack());
}
void MainWindow::addDocument(CadGraphicsView *view)
{
m_undoGroup->addStack(view->m_undoStack());
connect(view->m_undoStack(), SIGNAL(indexChanged(int)), this, SLOT(updateActions()));
connect(view->m_undoStack(), SIGNAL(cleanChanged(bool)), this, SLOT(updateActions()));
setCurrentDocument(view);
}
void MainWindow::setCurrentDocument(CadGraphicsView *view)
{
mdiArea->currentSubWindow();
}
CadGraphicsView *MainWindow::currentDocument() const
{
return qobject_cast<CadGraphicsView *>(mdiArea->parentWidget());
}
I am confused with why I am not able to push entities to undoStack. Please help me to solve this issue
I think the problem is in these two functions (see the inline comments):
void MainWindow::setCurrentDocument(CadGraphicsView *view)
{
// The view argument is not used at all. You do not set anything here.
mdiArea->currentSubWindow();
}
CadGraphicsView *MainWindow::currentDocument() const
{
// mdiArea->parentWidget() returns the MainWindow, so this function
// always returns 0.
return qobject_cast<CadGraphicsView *>(mdiArea->parentWidget());
// You should write it as
// return qobject_cast<CadGraphicsView *>(mdiArea->activeSubWindow()->widget());
}
I am trying to do undo/redo on multidocument interface but facing following error:
no matching function for call to 'qobject_cast(QMdiSubWindow*&)'
return qobject_cast<CadGraphicsView *>(activeSubWindow);
My code to above function is as follows:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
m_undoGroup = new QUndoGroup(this);
QAction *undoAction = m_undoGroup->createUndoAction(this);
undoAction->setShortcut(QKeySequence::Undo);
QAction *redoAction = m_undoGroup->createRedoAction(this);
redoAction->setShortcut(QKeySequence::Redo);
menuEdit->insertAction(menuEdit->actions().at(0), undoAction);
menuEdit->insertAction(undoAction, redoAction);
menuEdit->addAction(undoAction);
menuEdit->addAction(redoAction);
updateActions();
}
void MainWindow::setCurrentDocument()
{
mdiArea->currentSubWindow();
}
void MainWindow::addnewFile()
{
m_undoGroup->addStack(view->scene->undoStack());
connect(view->scene->undoStack(), SIGNAL(indexChanged(int)), this, SLOT(updateActions()));
connect(view->scene->undoStack(), SIGNAL(cleanChanged(bool)), this, SLOT(updateActions()));
}
void MainWindow::updateActions()
{
CadGraphicsScene *scene = currentDocument();
m_undoGroup->setActiveStack(scene == 0 ? 0 : scene->undoStack());
}
CadGraphicsView *MainWindow::currentDocument() const
{
if (QMdiSubWindow *activeSubWindow = mdiArea->currentSubWindow())
return qobject_cast<CadGraphicsView *>(activeSubWindow);
return 0;
}
void MainWindow::newFile()
{
// creates a new file
createMdiView();
view->newFile();
addnewFile();
curFileName = tr("Document %1").arg(++fileNumber);
view->setWindowTitle(curFileName);
view->scene->installEventFilter(this);
view->show();
isEntitySelected = false;
}
CadGraphicsView *MainWindow::createMdiView()
{
// creates a graphicsView and add it to the MDI window
view = new CadGraphicsView;
QMdiSubWindow *w = mdiArea->addSubWindow(view);
mdiArea->setActiveSubWindow(w);
windowViewList.append(qMakePair(w, view));
return view;
}
Can you please help me to solve the error. I have added the basic idea how it is working.
Got the solution to my problem. I need to change the currentDocument function as follows:
CadGraphicsView *MainWindow::currentDocument() const
{
return qobject_cast<CadGraphicsView *>(mdiArea->parentWidget());
}
I had a segmentation fault when using my new Qt Widget with Qt Designer 4.6. The problem arise when trying to preview the new widget.
when using gdb I found that the problem is in qdesigner_internal::WidgetFactory::applyStyleToTopLevel:
Program received signal SIGSEGV, Segmentation fault.
qdesigner_internal::WidgetFactory::applyStyleToTopLevel (style=0x0, widget=0x1829df0) at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp:777
777 /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp: No such file or directory.
in /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp
(gdb) bt
#0 qdesigner_internal::WidgetFactory::applyStyleToTopLevel (style=0x0, widget=0x1829df0) at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/widgetfactory.cpp:777
#1 0x00007ffff7475bed in qdesigner_internal::QDesignerFormBuilder::createPreview (fw=, styleName=..., appStyleSheet=..., deviceProfile=, scriptErrors=
0x7fffffffbee0, errorMessage=0x7fffffffc3f0) at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/qdesigner_formbuilder.cpp:404
#2 0x00007ffff7476773 in qdesigner_internal::QDesignerFormBuilder::createPreview (fw=0x0, styleName=..., appStyleSheet=..., deviceProfile=..., errorMessage=0x0)
at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/qdesigner_formbuilder.cpp:439
#3 0x00007ffff7532b27 in qdesigner_internal::PreviewManager::createPreview (this=0x837f20, fw=0x1879200, pc=..., deviceProfileIndex=-1, errorMessage=0x7fffffffc3f0, initialZoom=-1)
at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/previewmanager.cpp:686
#4 0x00007ffff75343cf in qdesigner_internal::PreviewManager::showPreview (this=0x837f20, fw=0x1879200, pc=..., deviceProfileIndex=-1, errorMessage=0x7fffffffc3f0)
at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/previewmanager.cpp:760
#5 0x00007ffff753472f in qdesigner_internal::PreviewManager::showPreview (this=0x837f20, fw=0x1879200, style=..., deviceProfileIndex=-1, errorMessage=0x7fffffffc3f0)
at /var/tmp/qt-x11-src-4.6.0/tools/designer/src/lib/shared/previewmanager.cpp:659
because a null pointer was passed there:
void WidgetFactory::applyStyleToTopLevel(QStyle *style, QWidget *widget)
{
const QPalette standardPalette = style->standardPalette();
if (widget->style() == style && widget->palette() == standardPalette)
return;
//....
}
I'm new in Qt and this is my first custom widget. does anybody have a clue for solving this.
here is my widget code
MBICInput::MBICInput(QWidget *parent) : QStackedWidget(parent){
displayPage = new QWidget();
displayPage->setObjectName(QString::fromUtf8("displayPage"));
inputLB = new QLabel(displayPage);
inputLB->setObjectName(QString::fromUtf8("inputLabel"));
inputLB->setCursor(QCursor(Qt::PointingHandCursor));
addWidget(displayPage);
EditPage = new QWidget();
EditPage->setProperty("EditInputLine", QVariant(true));
EditPage->setObjectName(QString::fromUtf8("EditPage"));
inputInput = new QLineEdit(EditPage);
inputInput->setGeometry(QRect(5, 10, 231, 25));
inputInput->setObjectName(QString::fromUtf8("input"));
addWidget(EditPage);
_animation = new QString("");
_message = new QString("Message");
_validator = new QRegExpValidator(QRegExp("[a-zA-Z]+"), this);
}
MBICInput::~MBICInput() {
}
QValidator::State MBICInput::validate(QString &text, int &pos) const{
return _validator->validate(text, pos);
}
void MBICInput::paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
}
QSize MBICInput::minimumSizeHint() const{
return QSize(200, 40);
}
QSize MBICInput::sizeHint() const{
return QSize(200, 40);
}
void MBICInput::setAnimation(const QString &animation){
*_animation = animation;
update();
}
QString MBICInput::animation() const{
return *_animation;
}
void MBICInput::setMessage(const QString &message){
*_message = message;
update();
}
QString MBICInput::message() const{
return *_message;
}
void MBICInput::mousePressEvent(QMouseEvent *event){
if(currentIndex()==0){
setCurrentIndex(1);
}else{
setCurrentIndex(0);
}
update();
}
In the applyStyleToTopLevel function, you are using the widget pointers without verifying it's a valid pointer. So when doing widget->style with a NULL widget pointer it will crash.
Just for information I just compiled the same code under the Qt version 4.5.3-9 and it worked just fine with the 4.5 designer. may be it's a Qt 4.6 bug...