Skip to content

General and Desktop

This is where all the desktop OS and general Qt questions belong.
83.6k Topics 457.7k Posts
  • How to adjust order of menu items?

    Solved
    7
    0 Votes
    7 Posts
    2k Views
    JonBJ
    @jronald said in How to adjust order of menu items?: Removing an action from menu will delete the action from memory, right? No. Untested, but did you see void QWidget::removeAction(QAction *action), @artwaw suggested that too: Removes the action action from this widget's list of actions. I would expect it to preserve everything for re-insert. removeAction and insertAction reference each other.
  • Add QXmlSchema to CMake

    Solved
    3
    0 Votes
    3 Posts
    420 Views
    P
    @Christian-Ehrlicher Thanks for answer.
  • Why does it draw headers like this?

    Solved
    7
    0 Votes
    7 Posts
    311 Views
    D
    Adding header is actually much simpler than what I thought. All I need is tableFormat.setHeaderRowCount(1); and add header just once and no need to calculate anything manually: [image: f145a634-2d29-4cbc-93fa-f714e4957766.gif] The only thing that bothers now is those horizontal lines, appears/disappears when I scroll. Here's the updated code: Window::Window(QWidget *parent) : QWidget(parent){ document = new QTextDocument(); document->setUndoRedoEnabled(false); printer = new QPrinter(); printer->setPageSize(QPageSize::A4); printer->setPageMargins(QMarginsF(72,72,72,72), QPageLayout::Point); printer->setResolution(fontMetrics().fontDpi()); // no need if you want bigger font auto pageRect = printer->pageRect(QPrinter::DevicePixel).size(); // set to QPrinter::Point if you want bigger font document->setPageSize(QSizeF(pageRect.width(), pageRect.height())); //document->documentLayout()->setPaintDevice(printer); createDocument(); auto preview = new QPrintPreviewWidget(printer); preview->setZoomMode(QPrintPreviewWidget::ZoomMode::FitToWidth); connect(preview, &QPrintPreviewWidget::paintRequested, [=](QPrinter *p){document->print(p);}); auto lay = new QVBoxLayout(this); auto button = new QPushButton("Pint PDF", this); lay->addWidget(button); lay->addWidget(preview); connect(button, &QPushButton::clicked, [=]{ printer->setOutputFormat(QPrinter::PdfFormat); printer->setOutputFileName("test.pdf"); document->print(printer); }); } void Window::addHeader(QTextCursor& cursor){ QTextTableCellFormat cellFormat; cellFormat.setTopBorder(1); cellFormat.setBottomBorder(1); cellFormat.setBorderBrush(Qt::black); cellFormat.setTopBorderStyle(QTextFrameFormat::BorderStyle_Solid); cellFormat.setBottomBorderStyle(QTextFrameFormat::BorderStyle_Solid); QTextCharFormat textFormat; textFormat.setFontWeight(QFont::Bold); QTextBlockFormat blockFormat; blockFormat.setAlignment(Qt::AlignCenter); cursor.setBlockFormat(blockFormat); for (int i = 0; i < 4; i++) { auto cell = cursor.currentTable()->cellAt(0, i); cell.setFormat(cellFormat); cursor.insertText("Column " + QString::number(i + 1), textFormat); cursor.movePosition(QTextCursor::NextCell); } } QTextTableFormat Window::format(){ QTextTableFormat tableFormat; tableFormat.setBorder(0); tableFormat.setCellSpacing(0); tableFormat.setCellPadding(0); tableFormat.setHeaderRowCount(1); QVector<QTextLength> columnLengths; columnLengths.append(QTextLength(QTextLength::PercentageLength, 40)); columnLengths.append(QTextLength(QTextLength::PercentageLength, 20)); columnLengths.append(QTextLength(QTextLength::PercentageLength, 20)); columnLengths.append(QTextLength(QTextLength::PercentageLength, 20)); tableFormat.setColumnWidthConstraints(columnLengths); return tableFormat; } void Window::createDocument(){ int row = 100, column = 4; QTextCursor cursor(document); cursor.insertTable(1, 4, format()); addHeader(cursor); for (int r = 1; r < row; r++) { cursor.currentTable()->appendRows(1); cursor = cursor.currentTable()->rowStart(cursor); cursor.movePosition(QTextCursor::NextCell); for (int c = 0; c < column; c++) { if(c == 0){ QString col1Text; if(r % 2 == 0) col1Text = "Column1 Text Column1 Text Column1 Text Column1 Text Column1 Text Column1 Text Column1 Text"; else col1Text = "Some other Column1 Text text text"; cursor.insertText(col1Text); cursor.movePosition(QTextCursor::NextCell); continue; } cursor.insertText("Row " + QString::number(r) + " Column " + QString::number(c)); cursor.movePosition(QTextCursor::NextCell); } } }
  • 0 Votes
    4 Posts
    287 Views
    M
    I'm pretty sure that your if statements are wrong. It's up to you to redraw all the elements that intersect with the redraw region.
  • prototype of signal QAction::triggered

    Solved
    5
    0 Votes
    5 Posts
    408 Views
    J.HilkJ
    @jronald do yourself a favour and do not use the connectSlotByName (void on_<object name>_<signal name>(<signal parameters>);) feature, but rather use the Qt5 connect syntax and connect signals to slots by hand https://doc.qt.io/qt-5/qmetaobject.html#connectSlotsByName https://wiki.qt.io/New_Signal_Slot_Syntax https://doc.qt.io/qt-5/signalsandslots.html its far less error prone
  • This topic is deleted!

    Unsolved
    1
    0 Votes
    1 Posts
    4 Views
    No one has replied
  • QListWidget drag indicator not showing

    Unsolved
    3
    0 Votes
    3 Posts
    563 Views
    L
    @JonB Thanks for reply. I have read this Using Drag and Drop with Item Views document carefully, but the method it metioned just not working. I have changed code to follow: setDragEnabled(true); setAcceptDrops(true); setDropIndicatorShown(true); setDefaultDropAction(Qt::MoveAction); this->setDragDropMode(QAbstractItemView::InternalMove); this->viewport()->setAcceptDrops(true); But it still no change, when I drag item, I can see a black dot drawn on top left corner of list view: [image: 1267b4f4-d962-44c5-b79e-2ffb897e408d.png] But not fully drawn as line and rectangle which encapsulate list items. @JonB Is there any simple example project to showcase drag and drop indicator works?
  • move indicator of QRadioButton to right side

    Solved qradiobutton
    3
    0 Votes
    3 Posts
    548 Views
    Joe JohnsonJ
    @J-Hilk It works,thanks
  • How to find a line number after an Enter key press in QTextEdit.

    Solved
    8
    0 Votes
    8 Posts
    754 Views
    J
    @mpergand Thank you. This way it works good. QTextCursor cursor= ui->textEditTerminalTx->textCursor(); QTextBlock tb = cursor.block(); QString line = tb.text(); Always get the last line above the cursor. Exactly what I need.
  • long menu entry displays partially

    Solved
    3
    0 Votes
    3 Posts
    418 Views
    jronaldJ
    @JonB said in long menu entry displays partially: @jronald In Qt6 I suspect you might be under Menu item with mnemonic gets cut off? https://bugreports.qt.io/browse/QTBUG-94481 ? Looks like you will need 6.3. Yes, exactly. Thanks
  • Creating a new thread with QtConcurrent did not work

    Unsolved
    6
    0 Votes
    6 Posts
    869 Views
    M
    Yes, its my mistake. I read over this section in the docs und had searched for other sources... :-/
  • Is possible to change that qmake uses by default /usr/bin/gcc?

    Solved
    5
    0 Votes
    5 Posts
    2k Views
    S
    There are two ways I see to make you opt in to these settings explicitly, so that it is specific to your machine (another Linux machine might be configured differently): Use an additional config variable which you specify when compiling on your machine. Inside the .pro file: LINUX_GCC11_WORKAROUND { QMAKE_CC = gcc-11 QMAKE_CXX = g++-11 } Inside QtCreator in the projects settings for the qmake step specify CONFIG+=LINUX_GCC11_WORKAROUND as additional argument. On the command line just add it to your options: qmake CONFIG+=LINUX_GCC11_WORKAROUND [...] MyProject.pro This is quite similar to @sierdzio's suggestion. But it is more explicit if other Linux machines are configured differently. My preferred method for this is a user-specific config.pri. This is what we have in our .pro files: exists(config.pri): include(config.pri) Every user can specify whatever they like inside the config.pri. This file will not be checked in to version control so that everyone can keep their own settings. Nobody ever has to know about your machine specific settings. If there are a dozen workarounds for different machines your .pro file will not be cluttered up by all the different workarounds like in version 1. I described. The two approaches can also be combined: If you want to keep track of all workarounds (maybe put them in a workarounds.pri checked into the version control) you can add CONFIG+=LINUX_GCC11_WORKAROUND into your config.pri to enable them on your machine only.
  • not giving error for the undefined class

    Unsolved
    6
    0 Votes
    6 Posts
    468 Views
    S
    @jsulm thank you i just read about the template parameters now only after you mentioned all this time i was thinking StorageType is a class name thank you for your time
  • Same application executed as console app or QML GUI

    Unsolved
    11
    0 Votes
    11 Posts
    993 Views
    S
    Windows does not support the same application to be both a console and a GUI app. The major problem is that a GUI application should not have an attached console and a console application should use an existing console when started from the command line instead of opening a new one. This is not possible on Windows. Here is the next best thing you can do on Windows (I have read that Microsoft's own devenv command does it this way): Compile your application both as .exe (GUI application) and .com (console/command line application). This can be done from the same source code if you use a few #ifdef's. If you have a myapp.exe and a myapp.com and type myapp (without the file extension) on the command line, Windows will do the right thing. In our own application we provided an option to specify you want the GUI. If the console app was started with the GUI option it will just use QProcess to launch the GUI version and exit. Here is a short snippet with the most important information how we do it: // Windows header #include <windows.h> #include <consoleapi.h> #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable : 4100) // false warning of unused parameter 'a' #endif void startGui(QApplication &a) { QString currentDir = QDir::currentPath(); QStringList arguments = a.arguments(); arguments.removeFirst(); QDir::setCurrent(QCoreApplication::applicationDirPath()); QProcess::startDetached(QFileInfo(QCoreApplication::applicationFilePath()).completeBaseName() + ".exe", arguments, currentDir); } #ifdef _MSC_VER #pragma warning(pop) #endif void openConsole() { AllocConsole(); std::freopen( "CON", "w", stdout ); std::freopen( "CON", "w", stderr ); std::freopen( "CON", "r", stdin ); } int main(int argc, char *argv[]) { QApplication a(argc, argv); CommandLineArgs args = parseCommandLine(a); #ifdef CONSOLE_APP if(args.gui) { startGui(a); return 0; } #else if(args.gui) { QString filename; Dialog w(filename); w.show(); return a.exec(); } openConsole(); #endif return processWithArgs(args); } The difference is that we don't have an event loop when run as a pure console application.
  • A thread execution problem.

    Solved
    10
    0 Votes
    10 Posts
    381 Views
    J
    @KroMignon Thank you for pointing me out. I went to SCRIPT::Run and I see it takes a long time to process - some sensors don't respond and I wait some timeout (3 sec) so it accumulates to 8-10 seconds of timeout.
  • QPlainTextEdit and fork + exec occur memory lack.

    Unsolved
    2
    0 Votes
    2 Posts
    187 Views
    jsulmJ
    @kdkim said in QPlainTextEdit and fork + exec occur memory lack.: Does fork + exec have an effect on memory shortage? Well, it starts a new process which will consume RAM. How much depends on what this process is executing. "Print many strings to QPlainTextEdit under preconditions without limit of maximum block count; also periodically call clear(). Does this sequence affect memory shortages?" - it all depends on how much stuff you put into your text edit before clearing it. Without more information not possible to answer. "Does ENOMEM occur in the execution of fork + exec by method 2." - if you are out of RAM then yes. But how should we answer this question without knowing more? Why are you asking this? Do you have any real issues with memory usage?
  • How to call this kind of UI?

    Solved
    3
    0 Votes
    3 Posts
    228 Views
    J.HilkJ
    the repo linked by @mrjj is probably well suited for your case but for your information: the actual name is "drawer menu". In QWidgets there is no default widget with that behaviour, but QML does have that component ready to use.
  • A UDP socket problem.

    Solved
    11
    0 Votes
    11 Posts
    1k Views
    Kent-DorfmanK
    what you are doing is generally a bad idea unless you are aggregating channels for redundancy or to increase badnwidth....in which case you'd have a bridge interface and do your bind() there. I don't understand why you are doing this in the first place since it complicates network routing, with minimal to no gain. I'm kind of breaking my own rule about not questioning the "reason" a person asks a question, but I'm at a loss as to why you think you need to do this. FWIW, you can assign multiple IPs to a single NIC is that is what you need.
  • howto: redirect console output to syslog

    Solved
    6
    0 Votes
    6 Posts
    2k Views
    A
    The solution that works for me (your mileage may vary) is to call qInstallMessageHandler() from the Qt Core package and supply my own QtMessageHandler function. There's a nice example of this in the help docs. https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler
  • QList of pointers - how to prevent memory leak.

    Solved qlist
    3
    0 Votes
    3 Posts
    670 Views
    artwawA
    @JonB Thank you!