QT - How to synchronize data between two widgets? - qt

I have a QT program with a custom widget that I'm implementing. This widget is an Hexeditor, and it's already functional.
But now I want to put on the window another instance of the same Hexeditor, and I want to synchronize the data between them, so if I change a byte in one Hexeditor, the same byte is automatically changed on the other Hexeditor.
What is the best solution for this problem? What are my options? Sometimes the files can be very big, so I'm trying to find the best solution.
This is a general question, and probably is valid for other text widgets.
Thanks

I would use the very good MVC architecture widgets in Qt.
As a hexeditor is generally laid out as a table, I would use QAbstractTableModel to actually store the hex data, and then QTableView to display the data. You can then have an arbitrary mount of hex editors connected to the same model and all will be synchronised automatically. You will also have to modify a QStyledItemDelegate to provide hex-only editing in the view, but that's really straightforward.

Related

Is it possible to create a Qt customer widget *in* designer *for* designer?

I have found a really neat custom widget for Qt and managed to incorporate it into Qt Designer. I would now like to create a customer widget in designer by placing 32 of these LEDs into a new widget in Designer, then add some functions to it such as giving it address and passing data into it that will then illuminate patterns in colours according to the passed data.
My questions are:
Can I do this? I have placed 32 LEDs on to a new QFrame widget in designer and named the LEDs 0-31, but am at a loss as to know what to do next.
Is there a tutorial anywhere for this? I have found loads of hits on google for creating custom widgets for designer, but not using designer.
Please bear in mind I am not a good Qt programmer, I write embedded code for micros in C and am really not good at object orientated code; I can just hack together relatively simple programs for test and measurement. I need something that will hand hold me through this.
http://doc.qt.io/qt-5/qtdesigner-worldtimeclockplugin-example.html
This seemed to be the kind of thing I wanted, it seemed to look like someone has placed widgets on a screen to create a new widget, but it seems to keep referring back to Creator.
Maybe I am trying to do something impossible. Maybe I have to place this new LED in creator with code?
Many thanks for any pointers.
Yes, it is. A Designer plugin basically consists of 2 Parts:
The Widget that you want to add to the designer
The plugin itself that provides information about the widget usw.
You can simply create the widget with the designer, add the QDESIGNER_WIDGET_EXPORT macro and thats it. Create your designer plugin to load this widget and it should work.
Note: Since you mentioned your not very experienced, you should try to follow the Custom Widget Plugin Example. Once you understood that one, it's only a small step to your designed plugin.

Qtablewidget or Qtableview, which is more proper to make a downloading task list?

Qtablewidget or Qtableview, which is more proper to make a downloading task list ?
I want to make a downloading task list like this
http://qt-project.org/doc/qt-4.8/images/widgetdelegate.png
the first question is : Qtablewidget or Qtableview, which is more suitable for this job ?
the second question is : how to draw a progress bar in the downloading task list ?
P.S. if chosing Qtablewidget ,I know it has a
QTableWidget.setCellWidget(row, column, widget)
method ,so we can use it to set QprogressBar there .
if chosing Qtableview, we can use Delegate like here
http://qt-project.org/doc/qt-4.8/qabstractitemdelegate.html
(maybe QStyleOptionProgressBarV2 will be more appropriate for the current Qt )
the same question comes here again :which way is more appropriate to do this ?
can anyone give a little example ? cuz the the Qt Torrent Example is so complicated for a newbie like me to learn from it .thanks !!!
Up to me it's very interesting discussion topic. If you look carefully in the documentation you will see that QTableWidget is actually a QTableView with specialised table-oriented functions, default table model, support for widgets in cells out of the box etc. It still has everything which QTableView has if you need some low level customisation. So you really don't have to choose. If you think (and looking on screenshots examples I would agree with that) that QTableWidget can handle most of things you need (like progress bars etc) just take it and go ahead with setCellWidget(...). Since it's QTableView anyway you always have an option to substitute it with your own QTableView implementation with fancy delegates etc.
I think in your scenario you you will use very limited amount of QTableWidget specialised functions, so that shouldn't be any problems at all.
Making it short, on your place I would go for QTableWidget.

C++ Qt - Hierarchy in the elements added to a GUI

In general, is the order in which I add my widgets into each other, the same order I access them back?
Example:
If I have a bunch of QPushButton in a QHBoxLayout, and this layout in a Window::ui,
can I access those button by simply ui->button_name? or Do I must do ui->layout->itemAt(idx)?
EDIT: My question aims to find an easy way to access elements that are deep into the hierarchy, like a label in a frame, inside a layout, inside a frame, inside the window etc...
PS: Also, I would really appreciate any documentations about good practices of GUI architecture!
Use ui->object_name you don;t need to worry about the layout!
It's possible to redesign the layout, eg. for different platforms, without changing any of the C++ code.
There are a couple of good books on Qt and the sample code is very good.
C++ GUI Programming with Qt 4
Advanced Qt Programming

Qt best practice: append signals and slots to my main window or create new class

Im just pondering best practice with an application I'm developing. Its a simple one window application using qt creator. It's just going to start a QProcess and show the output in a QTextEdit box. To do this there needs to be a bit of processing between the output of the QProcess and the QTextEdit but i dont know where i should do this, should i create a new class to do that or add member functions and extra signals and slots to my main window? I dont want mainwindow to become bloated and hard to read but equally i dont want to have more source files than really I need.
Any thoughts?
The main window class can very easily get bloated with all kinds of functionality. I've dealt with that myself, so it's a very real trouble.
Really, though, this is not so much of QT question as it is an object-oriented design question. The key is that your output window does not need to be a part of QMainWindow, so it probably shouldn't be. Make the display a widget, and insert it onto the main window. That is much more flexible, as if you ever need to move that output pane for any reason, it won't be coupled to a specific part of the program.
The logic that feeds data into that output pane should also get its own class, separating the responsibility for displaying the output from the responsibility for acquiring the output.
For reference on the concepts behind my suggestion, see the Single Responsibility Principle and the separation of concerns.
Also - you may wish to have a read at this link to model view delegate information which is how I tend to develop in Qt. Like the person above says - this is more a question of good OO design.

Qt - subclassing to provide an alternative view for a text widget?

Currently, I am in the design phase of a Qt widget like what one would see in a typical hex editor. It seemed simple enough to begin with, but as I dig into its implementation details I’m having some confusion.
Basically, the widget would consist of 3 core components: It will inherit QAbstractScrollArea or QScrollArea to provide scrolling and, in the viewport margin, it will display the file offset of each line. Then there will be two text editors; one with the hexadecimal value of each byte of the file, and one with the plaintext character representation.
I, of course, first checked qt-apps.org for any existing widgets, but a search for “hex” only returned QHexEdit and qPHexEditor, neither of which are very complete. I then considered creating a widget completely from scratch as they had, but felt like there should be a more elegant solution. Qt already has much text-editor functionality built into QTextEdit and QPlainTextEdit; why reinvent the wheel?
Now, while the “plaintext view” would be as simple as using a QPlainTextEdit with a fixed-width font and a width of 16 characters, the “hex view” is giving me a headache. I’ve been poring over QTextEdit, QAbstractTextDocumentLayout, etc., trying to figure out a way to present the desired appearance. For those who have never used a hex editor, it should function like so: – Using a fixed-width font, widget should be the width of 47 characters – Widget should display 2 hexadecimal characters per byte, with a blank space between bytes—-16 bytes per line
Since that thought, I’ve been trying to figure out how to subclass any related classes to provide the desired formatting. Unfortunately, the text editing classes don’t seem to follow the model/view framework as closely as I’d hoped, so deriving a new “view” for it doesn’t seem easy. Ideally, the widget would function like so:
One document/model for both the “hex” and “plaintext” views. Editing either view would adjust this model and update the other view appropriately. Signals/Slots at its best.
Because QTextEdit and QPlainTextEdit already provide much of the functionality needed (visible cursor, selections, undo/redo, native look and feel, etc), it would be ideal to re-use this.
So, does anyone have any recommendations? I appreciate any input on this.
QHexEdit2 is a quite complete editor widget for binary data. It can edit very big files, is available for Qt4, Qt5, PyQt4, PyQt4 with python 2 and 3.
see https://github.com/Simsys/qhexedit2

Resources