Skip to content

General and Desktop

This is where all the desktop OS and general Qt questions belong.
83.6k Topics 457.6k Posts
  • QStyledItemDelegate how to force close the editor?

    Unsolved
    4
    0 Votes
    4 Posts
    394 Views
    Christian EhrlicherC
    @StudentScripter said in QStyledItemDelegate how to force close the editor?: but sadly this way my delegates editevent logic isnt working anymore when clicking on this area. What does this mean? clickPosition is relative to the view, not relative to the current cell (how should it be?).
  • on screen logging

    Solved
    14
    0 Votes
    14 Posts
    2k Views
    W
    @SimonSchroeder thank you very much for your help. It is more clear now to me the use of static keyword in global variables which I did not understand. I have moved the static call to myMessageHandler, also checked if file is already open. So everything is safer and more meaningful now. thanks again for your help!
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    5 Views
    No one has replied
  • Question Concerning QWidget API

    Unsolved
    3
    0 Votes
    3 Posts
    277 Views
    F
    @Christian-Ehrlicher Sorry ,I'm a new user of Qt Forum .Please forgive my untidy code(
  • Moc not generating functions

    Solved
    18
    0 Votes
    18 Posts
    1k Views
    R
    Solution was to create SUBDIR .pro with lib and test subdirs. In lib I build a .dll from app and include the dll in test [image: 6e3c257a-a4f9-4f9d-b56e-4c4488f95778.png]
  • How to change center of scene/view?

    Unsolved
    3
    0 Votes
    3 Posts
    996 Views
    A
    @ChrisW67 You can see in this image I have four items and I calculated center of scene as A and clicked point as B [image: 260e731e-7715-4dbc-a07d-d78eef7f04da.png] After clicking on point B A new item added and that cause shifting of point B towards left . [image: c06dac09-bc6d-4e5e-9e00-9dc3a1287d01.png] Here what I want is that I want to match Poinit A with point C or Point B with point D by shifting the scene.. Here I am trying to set point A as center of scene so that it will move item toward right and then point B will shift on point D. @Qt-embedded-developer @qt_learning @qtgraphical @develop-world
  • Is QList thread-safe for certain operations?

    Solved
    6
    0 Votes
    6 Posts
    913 Views
    S
    @deisik said in Is QList thread-safe for certain operations?: So is QList thread-safe for modifying one of its elements (with replace()) from one thread while reading its other elements (with at()) from another? This isn't entirely about thread-safety of QList anymore. If you are storing ints inside the QList, at() will retrieve either the old value or the new value (assuming the ints are properly aligned) when do a replace() at the same time on the same index. If you store your own class in the container, your class would have to be somewhat thread-safe as well. However, I don't know an easy mechanism for locking assignment to the object (i.e. replacing its contents) against any access to the object while it is being changed. (Unless you synchronize all access to the object with a mutex.) If you store pointers to objects inside your container you get entirely different problems. Replacing a pointer is as safe as replacing an int. But, you might have a local copy of a pointer to the object which will now be replaced inside the QList. Most likely, the object will then also be destroyed by the thread that replaced the object. The local copy of the pointer is now invalid. Using shared pointers might help in this context, but they will slow down every access to the objects inside your container. (And you need to make sure that you get a copy of the shared pointer. The default implementation of at() will return a reference to whatever is stored inside the container, not a copy.)
  • Speed up make or/and ninja building proccess

    Unsolved
    8
    0 Votes
    8 Posts
    1k Views
    S
    @JacobNovitsky said in Speed up make or/and ninja building proccess: how to amend your project for ubuntu? Most of the things should work right away on Linux as well. The batch scripts (in the scripts folder) mostly work as a bash script also. grep and sed should be available by default on Linux or are easy to install through the package manager. Only generateInputFiles4Fbuild.bat needs a little more adjustment. You don't need to load Visual Studio (or any compiler for that matter) and just need to adjust the path to qmake. Also, rmdir needs to be replace by a simple rm -r. A little bit more involved is the adjustment of the .bff files. It should be fairly easy to adjust the paths of the .bff includes in the Qt subfolder. Certainly you need to replace the Visual Studio folder by a GCC or Clang folder. My inspiration for the general setup came from the .bff files for building FASTBuild itself. Have a look over there how to set up a GCC or Clang folder. The only additional thing they do is to copy the compiler explicitly to other machines during distributed builds. The toolchain (i.e. Windows or Linux, GCC or Clang (if you have both as option)) is selected in the fbuild.bff file. IIRC you can have a look at the FASTBuild setup itself for how to select the toolchain inside this file. One last thing is which compiler/linker flags to choose. For this I ran qmake once initially and had a look at the generated Makefile. These are the flags I set inside fbuild.bff and I think there are more (especially some default libs) inside the Visual Studio folder.
  • qt install problem

    Unsolved
    2
    0 Votes
    2 Posts
    127 Views
    sierdzioS
    @starlight make sure QT_DEBUG_PLUGINS is not defined.
  • axis setRange causes read acces violation

    Solved
    3
    0 Votes
    3 Posts
    257 Views
    W
    @Christian-Ehrlicher Exactly. You are right! Thank you for your help!
  • How to get git-commit ID during build and display it as version information

    Solved
    8
    0 Votes
    8 Posts
    3k Views
    Y
    @KH-219Design @JoeCFD @SGaist Thank you for nice solutions! I tried @JoeCFD 's solution. It worked and suitable for me!
  • Additional info upon compiling with Qt Creator

    Unsolved
    5
    0 Votes
    5 Posts
    404 Views
    C
    @JacobNovitsky said in Additional info upon compiling with Qt Creator: how to interpret below? file Static_lib.h Static_lib.h: C++ source, ASCII text The file command is inspecting the Static_lib.h file and, using its rules, determining that the file contains C++ source or ASCII text. This is indeed the case. This file is not the precompiled output file generated by the compilation at step 1. in my example. how to include -H to my building command, if I build program with Qt Creator? I would not bother. Run it a one file compile once manually, verify the result, and then forget about it. I you really must have this extra output in every compilation then add it to the project file's CFLAGS, something like this linux-g++: QMAKE_CFLAGS_DEBUG += -H
  • How assign the enter key to a button

    20
    0 Votes
    20 Posts
    30k Views
    C
    @fs_tigre great!!
  • QMap -> what does .contains() compare?

    Unsolved
    4
    0 Votes
    4 Posts
    641 Views
    Flaming MoeF
    Okay I was not aware, that == is the compare operator since others are also overloaded bool operator<=(QTime lhs, QTime rhs) bool operator==(QTime lhs, QTime rhs) bool operator>(QTime lhs, QTime rhs) bool operator>=(QTime lhs, QTime rhs)
  • Application is getting closed while emitting signal in QML file

    Unsolved
    1
    0 Votes
    1 Posts
    109 Views
    No one has replied
  • QTreeView prevent stacking indentation after certain childs.

    Unsolved
    8
    0 Votes
    8 Posts
    711 Views
    S
    @JonB oh sorry my oversight, here is the code of my delegate: delegate.cpp: #include "ViewLayerItemDelegate.h" #include <QStyledItemDelegate> #include <QPainter> #include <QApplication> #include <QItemSelectionModel> #include <QMouseEvent> #include "ViewLayerList.h" #include <QTimer> ViewLayerItemDelegate::ViewLayerItemDelegate(QObject *parent) : QStyledItemDelegate{parent} { } QWidget *ViewLayerItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { qDebug() << "Editor created"; LineEditCheckBoxWidget *editor = new LineEditCheckBoxWidget(parent); // Verbinden Sie das textChanged() Signal des LineEdits mit Ihrem Slot connect(editor->lineEdit, &QLineEdit::textChanged, this, &ViewLayerItemDelegate::onLineEditTextChanged); //Ein Timer um direkt die Editierung des LineEdit zu ermöglichen QTimer::singleShot(0, editor->lineEdit, SLOT(setFocus())); return editor; } void ViewLayerItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { LineEditCheckBoxWidget *widget = static_cast<LineEditCheckBoxWidget *>(editor); // Setzen Sie die Werte der SpinBox und CheckBox basierend auf den Modellwerten QString lineEditvalue = index.model()->data(index, Qt::EditRole).toString(); bool checkBoxValue = index.model()->data(index, Qt::CheckStateRole).toBool(); widget->lineEdit->setText(lineEditvalue); widget->checkBox->setChecked(checkBoxValue); widget->setFocus(); widget->lineEdit->setFocus(Qt::MouseFocusReason); // Editor aktivieren widget->lineEdit->setStyleSheet("background: white"); widget->iconLabel->setAttribute(Qt::WA_TranslucentBackground); //widget->setStyleSheet("background: white"); // Setzen Sie den bearbeiteten Index const_cast<ViewLayerItemDelegate*>(this)->setCurrentlyEditedIndex(index); } void ViewLayerItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { LineEditCheckBoxWidget *widget = static_cast<LineEditCheckBoxWidget *>(editor); // Speichern Sie die Werte der SpinBox und CheckBox im Modell QString lineEditvalue = widget->lineEdit->text(); bool checkBoxValue = widget->checkBox->isChecked(); model->setData(index, lineEditvalue, Qt::EditRole); model->setData(index, checkBoxValue ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole); qDebug() << "Model data set"; // Setzen Sie den bearbeiteten Index zurück const_cast<ViewLayerItemDelegate*>(this)->setCurrentlyEditedIndex(QModelIndex()); } void ViewLayerItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { editor->setGeometry(option.rect); } QSize ViewLayerItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { QSize size = QStyledItemDelegate::sizeHint(option, index); size.setHeight(40); // Setzen Sie hier die gewünschte Höhe // Ändern Sie die Einrückung basierend auf dem Level des Items (z.B., hier um 20 Pixel). int indentationLevel = index.column(); // Hier als Beispiel die Einrückung basierend auf der Spalte. size.rwidth() -= indentationLevel * 40; // Die Einrückung um 20 Pixel erhöhen. return size; } void ViewLayerItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem opt = option; initStyleOption(&opt, index); // Überprüfen Sie, ob der aktuelle Index bearbeitet wird if (index == currentlyEditedIndex) { return; } // Setzen Sie die Werte der SpinBox und CheckBox basierend auf den Modellwerten QString lineEditvalue = index.model()->data(index, Qt::EditRole).toString(); bool checkBoxValue = index.model()->data(index, Qt::CheckStateRole).toBool(); // Laden Sie das Icon und skalieren Sie es QPixmap iconPixmap("://resource/quick.png"); // Ersetzen Sie dies durch den Pfad zu Ihrer Icon-Datei QPixmap scaledPixmap = iconPixmap.scaled(32, 32, Qt::KeepAspectRatio, Qt::SmoothTransformation); // Berechnen Sie die Position für das Icon int centerY = option.rect.top() + option.rect.height() / 2; int iconY = centerY - scaledPixmap.height() / 2; QPoint iconPos = QPoint(option.rect.left() + 10, iconY); // Zeichnen Sie das Pixmap mit dem QPainter painter->drawPixmap(iconPos, scaledPixmap); // Berechnen Sie die Position und Größe für das LineEdit QRect lineEditRect = option.rect; lineEditRect.setLeft(iconPos.x() + scaledPixmap.width() + 10); // Adjust as needed lineEditRect.setRight(option.rect.right() - 10-35); // Adjust as needed // Erstellen Sie ein QStyleOptionFrame für das LineEdit QStyleOptionFrame lineEditOption; lineEditOption.lineWidth = 1; // Setzen Sie die Liniendicke auf 1 lineEditOption.rect = lineEditRect; // Zeichnen Sie das LineEdit QApplication::style()->drawControl(QStyle::CE_ShapedFrame, &lineEditOption, painter); // Überprüfen Sie, ob der Text leer ist und zeichnen Sie den Platzhaltertext // Zeichnen Sie den Text des LineEdits, considering truncation // Überprüfen Sie, ob der Text leer ist und zeichnen Sie den Platzhaltertext if (lineEditvalue.isEmpty()) { painter->drawText(lineEditOption.rect.adjusted(2, 0, 0, 0), Qt::AlignLeft | Qt::AlignVCenter, "<Empty>"); } else { // Hier wird die maximale Breite des Texts festgelegt, bevor er abgeschnitten wird int maxTextWidth = lineEditRect.width(); // Berechnen Sie die Größe des Texts QFontMetrics fm(painter->font()); QString displayedText = fm.elidedText(lineEditvalue, Qt::ElideRight, maxTextWidth - 10); painter->drawText(lineEditOption.rect.adjusted(2, 0, 0, 0), Qt::AlignLeft | Qt::AlignVCenter, displayedText); } // Berechnen Sie die Position und Größe für die CheckBox QRect checkBoxRect = option.rect; checkBoxRect.setLeft(lineEditRect.right() +35- 22); // Adjust as needed checkBoxRect.setRight(option.rect.right() - 10); // Adjust as needed // Erstellen Sie ein QStyleOptionButton für die CheckBox QStyleOptionButton checkBoxOption; checkBoxOption.state = checkBoxValue ? QStyle::State_On : QStyle::State_Off; checkBoxOption.rect = checkBoxRect; /* //CODE NUR ZUM VISUELLEN DEBUGGEN DER BEREICHE: // Zeichnen Sie ein rotes Rechteck um das LineEdit QPen redPen(Qt::red); painter->setPen(redPen); painter->drawRect(lineEditRect); // Zeichnen Sie ein rotes Rechteck um das LineEdit QPen bluePen(Qt::blue); painter->setPen(bluePen); painter->drawRect(checkBoxRect); */ // Zeichnen Sie die CheckBox QApplication::style()->drawControl(QStyle::CE_CheckBox, &checkBoxOption, painter); } bool ViewLayerItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { if(event->type() == QEvent::MouseButtonPress) { QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); if (const QTreeView* view = qobject_cast<const QTreeView*>(option.widget)) { const QRect itemRect = view->visualRect(index); ClickPosition = mouseEvent->position() - itemRect.topLeft(); // press position in item's coords //QRect itemRect = option.rect; qDebug() << "Click position: " << ClickPosition; qDebug() << "Item Rect: " <<itemRect; if(ClickPosition.x() >= itemRect.width()-35 && ClickPosition.x() <= itemRect.width()-20) { // Invert the state of the checkbox bool value = index.data(Qt::CheckStateRole).toBool(); model->setData(index, !value, Qt::CheckStateRole); } } } // Standardverhalten beibehalten für andere Ereignisse return QStyledItemDelegate::editorEvent(event, model, option, index); } // Slot-Implementierung void ViewLayerItemDelegate::onLineEditTextChanged(const QString &text) { // Hier können Sie die gewünschte Aktion ausführen, wenn der Text im LineEdit bearbeitet wird qDebug() << "LineEdit text changed to:" << text; } void LineEditCheckBoxWidget::mousePressEvent(QMouseEvent *event) { qDebug() << "Test clicked"; this->lineEdit->setStyleSheet("background: transparent;"); QWidget::mousePressEvent(event); } void ViewLayerItemDelegate::setCurrentlyEditedIndex(const QModelIndex &index) { currentlyEditedIndex = index; } delegate.h: #ifndef VIEWLAYERITEMDELEGATE_H #define VIEWLAYERITEMDELEGATE_H #include <QStyledItemDelegate> #include <QModelIndex> #include <QObject> #include <QSize> #include <QLineEdit> #include <QStandardItemModel> #include <QCheckBox> #include <QFormLayout> #include <QLabel> #include <QPushButton> // Definieren Sie das benutzerdefinierte Widget class LineEditCheckBoxWidget : public QWidget { Q_OBJECT public: QLineEdit *lineEdit; QCheckBox *checkBox; QLabel *iconLabel; LineEditCheckBoxWidget(QWidget *parent = nullptr) : QWidget(parent) { QHBoxLayout *layout = new QHBoxLayout(this); layout->setContentsMargins(10,0,20,0); layout->setSpacing(0); lineEdit = new QLineEdit(this); checkBox = new QCheckBox(this); iconLabel = new QLabel(this); // Laden Sie das Icon und setzen Sie es auf das QLabel QPixmap iconPixmap("://resource/quick.png"); // Ersetzen Sie dies durch den Pfad zu Ihrer Icon-Datei QPixmap scaledPixmap = iconPixmap.scaled(32, 32, Qt::KeepAspectRatio, Qt::SmoothTransformation); iconLabel->setPixmap(scaledPixmap); this->setStyleSheet("background-color: transparent"); layout->addWidget(iconLabel); layout->addSpacing(10); layout->addWidget(lineEdit); layout->addSpacing(10); layout->addWidget(checkBox); // Zugriff auf das QLineEdit-Widget und setzen Sie den Hintergrund transparent lineEdit->setStyleSheet("background-color: transparent;"); lineEdit->setFrame(false); lineEdit->setPlaceholderText("<Empty>"); checkBox->setStyleSheet("background-color: transparent"); //QPoint globalPos = checkBox->mapToGlobal(QPoint(0, 0)); //qDebug() << "Global position of checkbox on init: " << globalPos; } // QWidget interface protected: void mousePressEvent(QMouseEvent *event) override; signals: }; class ViewLayerItemDelegate : public QStyledItemDelegate { Q_OBJECT public: explicit ViewLayerItemDelegate(QObject *parent = nullptr); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override; QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override; QModelIndex currentlyEditedIndex; void setCurrentlyEditedIndex(const QModelIndex &index); //Speichert die Position wo geklickt wurde QPointF ClickPosition; public slots: void onLineEditTextChanged(const QString &text); signals: }; #endif // VIEWLAYERITEMDELEGATE_H
  • QTreeView can span children's items to a new item

    Unsolved
    7
    0 Votes
    7 Posts
    672 Views
    J
    @SGaist I read the qt example "Qt\Examples\Qt-6.5.1\widgets\itemviews\stardelegate". It uses StarDelegate which subclass QStyledItemDelegate, but the staritem size is controled by header. How a custom QStyledItemDelegate can expanding the item size that dones't control by header?
  • warn_unused_result | warning: ignoring return value of ‘int system(const char*)’

    Unsolved
    12
    0 Votes
    12 Posts
    3k Views
    JonBJ
    @JacobNovitsky said in warn_unused_result | warning: ignoring return value of ‘int system(const char*)’: @JonB because I see result and dont need in the context numeric result to be returned Of course you do need to look at the result. If it is non-0 you need to investigate and possibly report to user/outside world. For example, if it returns -1 that means creating the new process for the command to be executed fails. How can you not need to notice that? It is a recipe for having potentially bad behaviour at user runtime yet not indicating anything about it and then neither you nor user knows something has gone wrong. That is precisely why someone has bothered to mark the function warn_unused_result. If nothing else maybe do something like qDebug() the result when non-0. Rather than setting a compiler option to suppress warnings on any/all functions with warn_unused_result just for the sake of one system() call somewhere.
  • How to get speed network interface

    Unsolved
    5
    0 Votes
    5 Posts
    597 Views
    Christian EhrlicherC
    @fa_2 said in How to get speed network interface: Does anyone have a solution ? No, Qt has no option for this. You have to go low-level by yourself.
  • Do I need to include Q headers inner Q headers and to make these pre-compiled

    Locked Unsolved
    5
    0 Votes
    5 Posts
    344 Views
    Christian EhrlicherC
    Dupe of https://forum.qt.io/topic/150964/how-to-set-needed-permission-so-i-can-edit-qt-headers-from-qt-creator Please stop spamming the forum with the same question...