Overwriting the text in slot - windows

I am designing a chat application. My query is that I am printing the message and name of the user in a chat box on pressing a send button in Qt. Every time I press the button instead of writing the new message in next line, it erases the previous message and overwrites it by showing the new message only.
Code:
QString str = ui->textEdit->toPlainText();
QString name= ui->textEdit->objectName();
ui->textBrowser->setText(name);
ui->textBrowser->setText(name + ": " + str);
std::cout<<endl;

setText() sets the text in the textEdit (and not appends what you have probably guessed), therefore your second setText() overwrites the first one.
QString str = ui->textEdit->toPlainText();
QString name= ui->textEdit->objectName();
QString oldMsg = ui->textBrowser->text();
QString newMsg = name + ": " + str;
ui->textBrowser->setText(oldMsg + "\n" + newMsg);

Related

When split method doesn't work correctly than I expect?

String s = " hello ";
String[] strs = s.split(" ");
When I run this split method, I see that
Strs[0] = " ";
Strs[1] = " ";
Strs[2] = " ";
Strs[3] = " ";
Which makes sense so far.
However, even there is some " " after the word, "hello", java system did not creates any index for those of " " located after the word, "hello".
So, it eventually ended up to
Strs[0] = " ";
Strs[1] = " ";
Strs[2] = " ";
Strs[3] = " ";
Strs[4] = "hello";
Why is it??
This is a prime example of the two overloading methods of java split.
split(String regex) -
Javadoc
split(String regex, int limit) - Javadoc
When you use s.split(" "), the 2nd parameter limit is considered to be 0 by default, and this eliminates any dangling empty strings from result array.
If you want the dangling empty values, you have to explicitly pass -1 as the parameter like s.split(" ", -1) resulting in [, , , , hello, , ]
The limit parameter actually controls the number of times this pattern " " is applied. The Javadoc documentation links I shared has the technical details for further reading.

Adding multiple CC recipients in VBScript; Recipient.Type not working

I am working on a jsx (extendscript project) in which I am calling a VBS snippet with app.doScript. I cannot get the email_cc and email_cc2 recipients to show up as cc; they are stuck in the main "To" sending. I will not know in advance whether they exist in the users' address book, so I am adding them as recipients than trying to set their Type.
var vbs = 'Dim objOutl\r';
vbs += 'Set objOutl = CreateObject("Outlook.Application")\r';
vbs += 'Set objMailItem = objOutl.CreateItem(olMailItem)\r';
vbs += 'objMailItem.Display\r';
vbs += 'strEmailAddress = "' + email_address + '"\r';
vbs += 'objMailItem.Recipients.Add strEmailAddress\r';
vbs += 'strSubject = "' + the_subject + '"\r';
vbs += 'objMailItem.Subject = strSubject\r';
vbs += 'objMailItem.Body = "' + the_bodytext + '"\r';
if (email_cc && email_cc != "") {
vbs += 'Set cc1Recipient = objMailItem.Recipients.Add ("' + email_cc + '")\r';
if (email_cc2 && email_cc2 != "") {
vbs += 'Set cc2Recipient = objMailItem.Recipients.Add ("' + email_cc2 + '")\r';
vbs += 'cc1Recipient.Type = olCC\r';
vbs += 'cc2Recipient.Type = olCC\r';
}
else {
vbs += 'cc1Recipient.Type = olCC\r';
}
}
if (has_attachment) {
vbs += 'objMailItem.Attachments.Add "' + pdf_file + '"\r';
}
Check out the following points:
Try to use the fully qualified name in the code with the enum name, for example:
OlMailRecipientType.olCC
Use the Resolve method which attempts to resolve a recipient object against the address book. It returns true if the recipient was resolved.
Call the Save method to apply changes made through the OOM. Sometimes it makes sense to close the item, switch to another Outlook item, and then come back to check out results. Outlook caches changes and doesn't propagate changes made using the OOM immediately.
Read more about these methods and properties in the How To: Fill TO,CC and BCC fields in Outlook programmatically article.

Discord.js Announce Command Slice Issue with Args

iv made an announce command for my bot, i have it outputting the contents of the message to a RichEmbed.
I have it set in IF statement
If users mentioned first then it removes the mention and sends it to the channel and displays embed with contents (args.slice(2))
ELSE just send the embed with args.slice(1) removing the command.
My problem is that they either both slice(1) or both slice(2) , despite me having them set differently
if (!user);
const embed = new Discord.MessageEmbed()
.setTitle(`${(args.slice(1).join(" "))}`, 'https://i.gyazo.com/898806671312f3585209cf0fd69341006.jpg')
.setColor(0x320b52)
.setTimestamp()
.setFooter('Requested by ' + message.author.tag, 'https://i.gyazo.com/898806671312f3585209cf0fd69341006.jpg')
message.channel.send(embed);
}
else {
message.channel.send("<#" + user.id + ">")
const embed = new Discord.MessageEmbed()
.setTitle(`${(args.slice(2).join(" "))}`, 'https://i.gyazo.com/898806671312f358509cf0fd69341006.jpg')
.setColor(0x320b52)
.setTimestamp()
.setFooter('Requested by ' + message.author.tag, 'https://i.gyazo.com/898806671312f358509cf0fd69341006.jpg')
message.channel.send(embed);
}
Replace the ; in your first line with a {:
if (!user) {

How can I do a line break (line continuation) in Kotlin

I have a long line of code that I want to break up among multiple lines. What do I use and what is the syntax?
For example, adding a bunch of strings:
val text = "This " + "is " + "a " + "long " + "long " + "line"
There is no symbol for line continuation in Kotlin. As its grammar allows spaces between almost all symbols, you can just break the statement:
val text = "This " + "is " + "a " +
"long " + "long " + "line"
However, if the first line of the statement is a valid statement, it won't work:
val text = "This " + "is " + "a "
+ "long " + "long " + "line" // syntax error
To avoid such issues when breaking long statements across multiple lines you can use parentheses:
val text = ("This " + "is " + "a "
+ "long " + "long " + "line") // no syntax error
For more information, see Kotlin Grammar.
Another approach is to go for the 3 double quotes "" pairs i.e. press the double quotes 3 times to have something like this.
val text = """
This is a long
long
long
line
""".trimIndent()
With this approach you don't have to use + , \n or escape anything; just please Enter to put the string in the next line.
trimIndent() to format multi-line strings - Detects a common minimal indent of all the input lines, removes it from every line and also removes the first and the last lines if they are blank (notice difference blank vs empty).

Image Transmit to Intermec PM4i printer and then Print

I'm using Fingerprint to upload and then print image with pcx format.
Step1 Upload image to printer using TCP port, I use command :
IMAGE LOAD "bigfoot.1",1746,""\r\n
The printer returns with message "OK".
And then I send bytes data of bigfoot.1 to printer using socket.
Step 2 Print the image "bigfoot.1":
PRPOS 200,200
DIR 3
ALIGN 5
PRIMAGE "bigfoot.1"
PRINTFEED
RUN
The problem comes, the printer returns with message "Image not found". So I come up with the possibility of failure of upload. So I open the software PrintSet4 to check the image, the image already exists in TMP.Odd!!!
At last, I used PrintSet4 to substitute my socket application to upload image, After add file and apply, I use the step2 print command to print image, It works fine!
Here is the C# code to upload Image:
public void SendFile(string filePath, string CR_LF)
{
FileInfo fi = new FileInfo(filePath);
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] byteFile = new byte[fs.Length];
string cmd = "IMAGE LOAD \"" + fi.Name + "\"," + byteFile.Length.ToString() + ",\" \"" + CR_LF;
ClientSocket.Send(encode.GetBytes(cmd));
fs.Read(byteFile, 0, byteFile.Length);
Thread.Sleep(1000);
ClientSocket.Send(byteFile);
}
}
I have modified your code and used serial port.
public void SendFile(string filePath)
{
SerialPort port = new SerialPort("COM3", 38400, Parity.None, 8, StopBits.One);
port.Open();
FileInfo fi = new FileInfo(filePath);
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] byteFile = new byte[fs.Length];
// string cmd = "IMAGE LOAD \"" + fi.Name + "\"," + teFile.Length.ToString()+ ",\"\"" + CR_LF;
string cmd = "IMAGE LOAD " + "\"" + fi.Name + "\"" + "," + byteFile.Length.ToString() + "," + "\"S\"";
port.WriteLine(cmd);
fs.Read(byteFile, 0, byteFile.Length);
port.Write(byteFile,0,byteFile.Count());
int count = byteFile.Count();
int length = byteFile.Length;
}
}
So I noticed the problem was using CR_LF. Instead, I used port.WriteLine(cmd), which acts the same as adding a line separator. And it worked fine.

Resources