I created so much qpushbutton to represent the seat in the cinema. After user buy the seats I made these seats disabled. all I want to do is to see previously disabled button disabled. I saved this disabled button to a txt file and read their name but I could not assign it as my widget Qpushbuttons. Is there a way to solve it?
This is not as much a button issue as it is a data structure issue. You should somehow connect your buttons/seats to a data structure which aids in the bookkeeping of available and reserved seats. Once you close the program, you write out the data to a file or database, which you can subsequently read again when you open your application. You can then disable the buttons again of those seats which are reserved.
I've made some quick example with Qt, I hope this will help you:
// list of all seats in order (true means seat is taken, false seat is still free)
QList<bool> seats;
// set some test values
seats.append(true);
seats.append(true);
seats.append(false);
seats.append(true);
// file where the seats will be stored
QFile file("seats.dat");
// save to file
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << seats;
file.close();
// remove all seats (just for testing)
seats.clear();
// read from file
file.open(QIODevice::ReadOnly);
QDataStream in(&file);
in >> seats;
file.close();
// simple debug output off all seats
qDebug() << seats;
// you could set the buttons enabled state like this
QList<QPushButton*> buttons; // list of your buttons in the same order as the seat list of course
for (int i = 0; i < seats.count(); ++i)
buttons[i]->setEnabled(!seats.at(i)); // disables all seats which are already taken
This is off course just a simple solution using a QDataStream to serialize the complete List of seats, but you can play around with this if you are new to Qt/C++
Related
I am using an embedded system to send data from 25 sensors to a putty terminal on my computer. Works great.
I wanted to add a read from terminal functionality to the embedded system (so I can send commands). So I tried using getchar() to read whatever I would write on my putty terminal. First I just wanted to getchar and print the character back on putty. It kinda works, but my sensor data, which is supposed to print every 500ms, does not print until I type a char in putty. It is as if my code was stuck on getchar() and stuck in a while loop until getchar() reads something.
Here is my forever loop in my int main(). I am not sharing the rest as it is not really needed and too bulky (its just initializing modules). In this loop I am reading a sensor, trying to read from putty, writing to putty, and starting my next scan:
for(;;)
{
CapSense_ProcessAllWidgets(); // Process all widgets
CapSense_RunTuner(); // To sync with Tuner application
read_sensor(curr_elem); //read curr_elem
(curr_elem < RX4_TX4)?(curr_elem++):(curr_elem = 0, touchpad_readings_flag++);
// Here is the part to read I added which blocks until I type in something.
// If I remove this if and all of what's in it, I print to putty every 500ms
if(touchpad_readings_flag)
{
char received_char = getchar();
if (received_char) //if something was returned, received_char != 0
{
printf("%c", received_char);
}
}
//Here I write to putty. works fine when I remove getchar()
if (print_counter_flag && touchpad_readings_flag)
{
print_counter_flag = 0;
touchpad_readings_flag = 0;
for (int i = 0; i < 25; i++)
{
printf("\n");
printf("%c", 97 + i);
printf("%c", val[i] >> 8);
printf("%c", val[i] & 0x00ff); // For raw counts
printf("\r");
}
}
/* Start next scan */
CapSense_UpdateAllBaselines();
CapSense_ScanAllWidgets();
}
Apparently, your getchar() call is blocking unless there is input data to retrieve.
One solution to change this behaviour has been given by another article on different SE board.
Please also note that getchar() is a wrapper for getc() that is acting on stdin as this site1 describes.
For getc() you find further discussions.
In one of those, it is pointed out that some important implementations even wait for a newline character until input is delivered to your function. I think this depends on standard libraries/kind of embedded system you actually use - please check the documentation of your toolchain vendor.2
1
I didn't look up a normative source, this is just my first google hit.
2
The question doesn't specify the kind of embedded system, so a generic answer is wanted instead of a discussion of particular target/toolchain combinations, IMO.
When you load a file into a QMediaPlayer instance, it does not automatically buffer the file. The MediaStatus remains NoMedia until you play the file using play(), only after which it will end up as BufferedMedia. I can't find any way in the documentation to force the player to buffer the file without playing it - is there any way to do this?
Right now I'm planning on muting it, playing the file, then stopping it again and unmuting it, but it makes me feel dirty. Surely there is a better way to do this?
This is necessary, by the way, because I can't retrieve the duration until the file has been buffered, and I need the duration to select a position in the track to start playing from.
Thanks
The MediaStatus remains NoMedia until you play the file using play()
Not in Qt 5, and its not the reason you can't know 'duration' and set 'position'.
When you set the media with setMedia it does not wait for the media to finish loading (and does not check for errors). a signal mediaStatusChanged() is emitted when media is loaded, so listen to that signal and error() signal to be notified when the media loading is finished.
connect(player, &QMediaPlayer::mediaStatusChanged, this, [=]() {
qDebug() << "Media Status:" << player->mediaStatus();
});
I need the duration to select a position in the track to start playing
from.
Once the media file is loaded and before play, you can check the duration and you can set the player to your desired position, but this is best to do once the duration changes from 0 to the media duration after loading, so connect to signal durationChanged():
connect(player, &QMediaPlayer::durationChanged, this, [&](qint64 duration) {
qDebug() << "Media duration = " << duration;
player->setPosition(duration/2);
qDebug() << "Set position:" << player->position();
});
I can't find any way in the documentation to force the player to
buffer the file without playing it - is there any way to do this?
Yes, create a buffer from file then set media content to your buffer (but this is not required to do the above, it just provides a faster way to seek in media):
QString fileName=QFileDialog::getOpenFileName(this,"Select:","","( *.mp3)");
QFile mediafile(fileName);
mediafile.open(QIODevice::ReadOnly);
QByteArray *ba = new QByteArray();
ba->append(mediafile.readAll());
QBuffer *buffer = new QBuffer(ba);
buffer->open(QIODevice::ReadOnly);
buffer->reset(); //seek 0
player->setMedia(QMediaContent(), buffer);
I’m working under kubuntu 12.10 and developping an application into which i need to generate some sound into a QIODevice, then play it with QAudioOutput.
I’ve read all the litterature around speaking of how to properly do that, and I think to have done so.
So far I’ve done :
QVector <double> * soundData = SoundGenerator::getSound();
soundBuffer->open(QIODevice::ReadWrite);
QDataStream writeStream(soundBuffer);
foreach(double d, *soundData) {
char value = d * (2 << 7);
// qDebug() << "Value : " << (short int)value;
writeStream << value;
}
QAudioFormat format;
// Set up the format, eg.
format.setSampleRate(SoundGenerator::getAudioSampleRate());
format.setChannelCount(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
audio = new QAudioOutput(format, this);
if (audio->error() != QAudio::NoError) {
qDebug() << "Problem playing sound";
}
connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(aboutToFinish(QAudio::State)));
I have also a call to
audio->start(soundBuffer)
—
from another slot
I do not have any error in the initialization of the QAudioOutput
And I have NO SOUND AT ALL (all other applications have sound, and I’m porting a Qt4 app to Qt5, in Qt4 everything is ok with Phonon)
The aboutToFinish slot is called at the beggining with ActiveState as state, and NoError when calling QAudioOutput::error, but it’s not called anymore, even if waiting far more than the expected generated sound duration.
The sound generation process is not to be put in question, it has been tested by writing wav files, and it works.
Moreover, I have built the multimedia example from Qt’s sources, when it comes to pure audio there is no output (for example in the sprectrum example), on another hand, video plays with the sound perfectly.
Is there any known issue concerning that ? Is that a bug ? Am I doing something wrong ?
Thanks in advance ;)
This does not work because you have set 8 bit sample size and signed integer format.
SOLUTION: You have to set the sample type to unsigned for 8-bit resolution:
format.setSampleType(QAudioFormat::UnsignedInt);
This is not a Qt bug. Why? The answer is that in the WAV spec', 8-bit samples are always unsigned, whereas 16-bit samples are always signed. Any other combination does not work.
So for 16-bit samples you would have to put:
format.setSampleType(QAudioFormat:SignedInt);
(IMHO the fact that Qt does not take care of handling these cases by forcing the correct format is a flaw but not a lack in functionnality).
You can learn more about this in the notes section of this page: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
And also the solution to this very similar question (same problem but with 16-bit): Qt QAudioOutput push mode
Try to add:
QEventLoop loop;
loop.exec();
I'm trying to use QNetworkAccessManager to upload http multiparts to a dedicated server.
The multipart consists of a JSON part describing the data being uploaded.
The data is read from a serial QIODevice, which encrypts the data.
This is the code that creates the multipart request:
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart metaPart;
metaPart.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
metaPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"metadata\""));
metaPart.setBody(meta.toJson());
multiPart->append(metaPart);
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(fileFormat));
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\""));
filePart.setBodyDevice(p_encDevice);
p_encDevice->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(filePart);
QNetworkAccessManager netMgr;
QScopedPointer<QNetworkReply> reply( netMgr.post(request, multiPart) );
multiPart->setParent(reply.data()); // delete the multiPart with the reply
If the p_encDevice is an instance of QFile, that file gets uploaded just fine.
If the specialised encrypting QIODevice is used (serial device) then all of the data is read from my custom device. however QNetworkAccessManager::post() doesn't complete (hangs).
I read in the documentation of QHttpPart that:
if device is sequential (e.g. sockets, but not files),
QNetworkAccessManager::post() should be called after device has
emitted finished().
Unfortunately I don't know how do that.
Please advise.
EDIT:
QIODevice doesn't have finished() slot at all. What's more, reading from my custom IODevice doesn't happen at all if QNetworkAccessManager::post() is not called and therefore the device wouldn't be able to emit such an event. (Catch 22?)
EDIT 2:
It seems that QNAM does not work with sequential devices at all. See discussion on qt-project.
EDIT 3:
I managed to "fool" QNAM to make it think that it is reading from non-sequential devices, but seek and reset functions prevent seeking. This will work until QNAM will actually try to seek.
bool AesDevice::isSequential() const
{
return false;
}
bool AesDevice::reset()
{
if (this->pos() != 0) {
return false;
}
return QIODevice::reset();
}
bool AesDevice::seek(qint64 pos)
{
if (this->pos() != pos) {
return false;
}
return QIODevice::seek(pos);
}
You'll need to refactor your code quite a lot so that the variables you pass to post are available outside that function you've posted, then you'll need a new slot defined with the code for doing the post inside the implementation. Lastly you need to do connect(p_encDevice, SIGNAL(finished()), this, SLOT(yourSlot()) to glue it all together.
You're mostly there, you just need to refactor it out and add a new slot you can tie to the QIODevice::finished() signal.
I've had more success creating the http post data manually than with using QHttpPart and QHttpMultiPart. I know it's probably not what you want to hear, and it's a little messy, but it definitely works. In this example I am reading from a QFile, but you can call readAll() on any QIODevice. It also is worth noting, QIODevice::size() will help you check if all the data has been read.
QByteArray postData;
QFile *file=new QFile("/tmp/image.jpg");
if(!(file->open(QIODevice::ReadOnly))){
qDebug() << "Could not open file for reading: "<< file->fileName();
return;
}
//create a header that the server can recognize
postData.insert(0,"--AaB03x\r\nContent-Disposition: form-data; name=\"attachment\"; filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n");
postData.append(file->readAll());
postData.append("\r\n--AaB03x--\r\n");
//here you can add additional parameters that your server may need to parse the data at the end of the url
QString check(QString(POST_URL)+"?fn="+fn+"&md="+md);
QNetworkRequest req(QUrl(check.toLocal8Bit()));
req.setHeader(QNetworkRequest::ContentTypeHeader,"multipart/form-data; boundary=AaB03x");
QVariant l=postData.length();
req.setHeader(QNetworkRequest::ContentLengthHeader,l.toString());
file->close();
//free up memory
delete(file);
//post the data
reply=manager->post(req,postData);
//connect the reply object so we can track the progress of the upload
connect(reply,SIGNAL(uploadProgress(qint64,qint64)),this,SLOT(updateProgress(qint64,qint64)));
Then the server can access the data like this:
<?php
$filename=$_REQUEST['fn'];
$makedir=$_REQUEST['md'];
if($_FILES["attachment"]["type"]=="image/jpeg"){
if(!move_uploaded_file($_FILES["attachment"]["tmp_name"], "/directory/" . $filename)){
echo "File Error";
error_log("Uploaded File Error");
exit();
};
}else{
print("no file");
error_log("No File");
exit();
}
echo "Success.";
?>
I hope some of this code can help you.
I think the catch is that QNetworkAccessManager does not support chunked transfer encoding when uploading (POST, PUT) data. This means that QNAM must know in advance the length of the data it's going to upload, in order to send the Content-Length header. This implies:
either the data does not come from sequential devices, but from random-access devices, which would correctly report their total size through size();
or the data comes from a sequential device, but the device has already buffered all of it (this is the meaning of the note about finished()), and will report it (through bytesAvailable(), I suppose);
or the data comes from a sequential device which has not buffered all the data, which in turn means
either QNAM reads and buffers itself all the data coming from the device (by reading until EOF)
or the user manually set the Content-Length header for the request.
(About the last two points, see the docs for the QNetworkRequest::DoNotBufferUploadDataAttribute.)
So, QHttpMultiPart somehow shares these limitations, and it's likely that it's choking on case 3. Supposing that you cannot possibly buffer in memory all the data from your "encoder" QIODevice, is there any chance you might know the size of the encoded data in advance and set the content-length on the QHttpPart?
(As a last note, you shouldn't be using QScopedPointer. That will delete the QNR when the smart pointer falls out of scope, but you don't want to do that. You want to delete the QNR when it emits finished()).
From a separate discussion in qt-project and by inspecting the source code it seems that QNAM doesn't work with sequential at all. Both the documentation and code are wrong.
I'm streaming a large file ( 1Gb ) via HTTP to my server in Qt on a very memory constrained embedded Linux device. When I first receive the header I determine where to write the data on the filesystem, create a QFile pointer to that location, and open the file for appending. There is an 'accumulate' function in the server that is called each time new data arrives to the socket. From that accumulate function I want to stream the data right to the file via write(). You can see my accumulate function below.
My problem is memory usage when doing this -- I run out of memory. Shouldn't I be able to flush() and fsync() each iteration of the accumulation and not have to worry about RAM usage? What am I doing wrong and how can I fix this? Thanks -
I open my file once before the accumulate function:
// Open the file
filePointerToWriteTo->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered)
Here is a portion of the accumulate function:
// Extract the QFile pointer from the QVariant
QFile *filePointerToWriteTo = (QFile *)(containerForPointer->pointer).value<void *>();
qDebug() << "APPENDING bytes: " << data.length();
// Write to the file and sync
filePointerToWriteTo->write(data);
filePointerToWriteTo->waitForBytesWritten(-1);
filePointerToWriteTo->flush(); // Flush
fsync(filePointerToWriteTo->handle()); // Make sure bytes are written to disk
EDIT:
I instrumented my code and the 'waitForBytesWritten(-1)' call ALWAYS return 'false'. The docs say this should wait until data is written to the device.
Also, If I uncomment only the 'write(data)' line, then my free memory never decreases. What could be going on? How does 'write' consume so much memory?
EDIT:
Now I am doing the following. I do not run out of memory, but my free memory drops to 2Mb and hovers there until the entire file is transferred. At which point, the memory is released. If I kill the transfer in the middle, the kernel seems to hold on to the memory because it stays around 2Mb free until I restart the process and try to write to the same file. I still think I should be able to use and flush the memory each iteration:
// Extract the QFile pointer from the QVariant
QFile *filePointerToWriteTo = (QFile *)(containerForPointer->pointer).value<void *>();
int numberOfBytesWritten = filePointerToWriteTo->write(data);
qDebug() << "APPENDING bytes: " << data.length() << " ACTUALLY WROTE: " << numberOfBytesWritten;
// Flush and sync
bool didWaitForWrite = filePointerToWriteTo->waitForBytesWritten(-1); // <----------------------- This ALWAYS returns false!
filePointerToWriteTo->flush(); // Flush
fsync(filePointerToWriteTo->handle()); // Make sure bytes are written to disk
fdatasync(filePointerToWriteTo->handle()); // Specific Sync
sync(); // Total sync
EDIT:
This kind of sounds like me misunderstanding Linux caching. After reading this post --> http://blog.scoutapp.com/articles/2010/10/06/determining-free-memory-on-linux, it's possible that I am misunderstanding the output of 'free -mt'. I have been watching the 'free' field in that output and see it drop to hover around 2MB on the massive file transfer. I would just like to see it return to high levels of free data when the file transfer is done.
I think Linux is just caching everything it can and frees what it can spare around the 2MB free memory limit. I do not run out of memory when receiving or sending out ~2Gb of files on a 512 MB RAM system. In my Qt program, after receiving all of the data, appending to file, and closing the file. I do the following in a QProcess to see my 'free' memory return in the 'free -mt' command in a separate terminal:
// Now we've returned a large file - so free up cache in linux
QProcess freeCachedMemory;
freeCachedMemory.start("sh");
freeCachedMemory.write("sync; echo 3 > /proc/sys/vm/drop_caches"); // Sync to disk and clear Linux cache
freeCachedMemory.waitForFinished();
freeCachedMemory.close();